4

When I try to use cron to execute my python script in a future time, I found there is a command at, AFAIK, the cron is for periodically execute, but what my scenario is only execute for once in specified time. and my question is how to add python script to at command, also it there some python package for control the at command

My dev os is ubuntu 10.04 lucid,and my product server is ubuntu-server 10.04 lucid version. in fact, I want through python script add python script tasks to at command, which file's change can effect at command add or remove new jobs

1
  • If you edit the Q to add the crucial info you completely skipped (what system are you running in, for example?!), we'll be glad to help. (No, no Python package that I've heard of). Commented Sep 23, 2010 at 1:58

4 Answers 4

4

This works on my linux box:

echo python myscript | at 10:15

Edit: stupid quoting...

Sign up to request clarification or add additional context in comments.

Comments

3

As the man page says, at (as opposed to cron for example) doesn't respect shebang (the #!/usr/bin/env python line). It always uses /bin/sh to run the file.

So in order to run a python script you have to use either

echo python myscript.py | at 10:15

as suggested by @bstpierre or create an additional file

myscript.sh:

python myscript.py

and then

at -f myscript.sh at 10:15

Shebangs are not necessary this way (but wouldn't hurt either).

1 Comment

One logical explanation of such strange behaviour that I could have thought of is that -f flag appeared later on and to keep backwards compatibility they decided to ignore the shebang (otherwise launching at with the same commands read from stdin and from the file specified by -f would give different results)
2

type man at, it will explain how to use it. Usage will slighty differ from system to system, so there's no use to tell you here exactly.

Comments

-1

Just do

python FILE | at TIME > app.log

replace: FILE - Your .py file (include the shebang)

TIME - Your time

5 Comments

i do what you give,it response me a warning,what's the hell? python /home/mlzboy/my/ide/test/c.py | at now+2 minutes >/home/mlzboy/haha.txt warning: commands will be executed using /bin/sh job 6 at Thu Sep 23 10:27:00 2010
@mizboy do you have the shebang, it seems to me that the terminal thinks what you are running is a batch script, either that or it is referring to it running the command as a batch script (in which case it is bash so that is fine)
i had add 1 #!/usr/bin/env python 2 #encoding=utf-8 at top of my py script
yet i have another relevent question how can i catch the pid of at command currently execute task to write to a file
This answer is wrong. It will execute the file now, see @bstpierre answer with correct usage.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.