User avatar
addeswe
Posts: 27
Joined: Fri Jul 19, 2013 3:49 pm
Location: Sweden

Alternative to at scheduler

Tue Mar 29, 2016 1:08 pm

Hi. I want to run a software at random times and for this, at would be the perfect solution. My only problem is that it runs all scripts with the shell /bin/sh. And most stuff I write is in Python3. So is there a way to set Python3 as the shell, or is there any alternative? And no. I don't want to use crontab for random. :-)

Thanks in advance.
“Knock, knock.” - “Who’s there?” - very long pause…. - “Java.”
If a python knocks on your door and says “Hi, I’m a vacuum cleaner!” it’s not true. That’s a python.

jbudd
Posts: 945
Joined: Mon Dec 16, 2013 10:23 am

Re: Alternative to at scheduler

Tue Mar 29, 2016 1:55 pm

Make the first line of your script

Code: Select all

#! /usr/bin/python3
Then the script will be executed by the Python interpreter not bash.

User avatar
addeswe
Posts: 27
Joined: Fri Jul 19, 2013 3:49 pm
Location: Sweden

Re: Alternative to at scheduler

Tue Mar 29, 2016 3:46 pm

Well... that's not really true. :-(

Code: Select all

[[email protected]][/tmp] more test.py 
#!/usr/bin/python3

with open('/tmp/thisisanewfile.txt','w') as file:
    file.write('This is working?!')

[[email protected]][/tmp] at 17:38 -M -f /tmp/test.py 
warning: commands will be executed using /bin/sh
job 2 at Tue Mar 29 17:38:00 2016
“Knock, knock.” - “Who’s there?” - very long pause…. - “Java.”
If a python knocks on your door and says “Hi, I’m a vacuum cleaner!” it’s not true. That’s a python.

tpylkko
Posts: 381
Joined: Tue Oct 14, 2014 5:21 pm

Re: Alternative to at scheduler

Tue Mar 29, 2016 3:50 pm

are you running these programs on Raspbian? If so, you can use systemd timers with random time.

User avatar
DougieLawson
Posts: 35789
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website Twitter

Re: Alternative to at scheduler

Tue Mar 29, 2016 4:13 pm

Just because the AT command says it's running stuff with /bin/sh doesn't mean it won't use the shebang line once /bin/sh has read the python program and tried to execute it. Here's an example run on Raspbian Jessie

Code: Select all

[email protected] ~/python $ cat for.at.py
#!/usr/bin/python3

with open('/tmp/thisisanewfile.txt','w') as file:
    file.write('Is this working from AT?')
[email protected] ~/python $ at 17:12
warning: commands will be executed using /bin/sh
at> /home/pi/python/for.at.py
at> <EOT>
job 17 at Tue Mar 29 17:12:00 2016
[email protected] ~/python $ date
Tue 29 Mar 17:12:03 BST 2016
[email protected] ~/python $ cat /tmp/thisisanewfile.txt
Is this working from [email protected] ~/python $
Note: Having anything humorous in your signature is completely banned on this forum. Wear a tin-foil hat and you'll get a ban.

Any DMs sent on Twitter will be answered next month.

This is a doctor free zone.

User avatar
addeswe
Posts: 27
Joined: Fri Jul 19, 2013 3:49 pm
Location: Sweden

Re: Alternative to at scheduler

Tue Mar 29, 2016 7:07 pm

Well ain't that a treat. Thanks DougieLawson for setting me on the right path.

Answer if somebody else experience the same problem:
You can't write it on one line: at 21:12 -M -f /tmp/whatever.py. This throws an error since it uses /bin/sh to execute whatever the shebang is.
Solution: See previous post from Dougie OR echo /tmp/whatever.py | at 21.12 -M

Thanks again for the support.
“Knock, knock.” - “Who’s there?” - very long pause…. - “Java.”
If a python knocks on your door and says “Hi, I’m a vacuum cleaner!” it’s not true. That’s a python.

Return to “Beginners”