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

Antony Hatchkins
  • 31,947
  • 10
  • 111
  • 111
mlzboy
  • 14,343
  • 23
  • 76
  • 97
  • 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). – Alex Martelli Sep 23 '10 at 01:58

4 Answers4

4

This works on my linux box:

echo python myscript | at 10:15

Edit: stupid quoting...

bstpierre
  • 30,042
  • 15
  • 70
  • 103
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).

axil
  • 1,558
  • 16
  • 17
  • 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) – axil Aug 06 '15 at 09:46
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.

knitti
  • 6,817
  • 31
  • 42
-1

Just do

python FILE | at TIME > app.log

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

TIME - Your time

Zimm3r
  • 3,369
  • 5
  • 35
  • 53
  • 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 – mlzboy Sep 23 '10 at 02:27
  • @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) – Zimm3r Sep 23 '10 at 02:39
  • i had add 1 #!/usr/bin/env python 2 #encoding=utf-8 at top of my py script – mlzboy Sep 23 '10 at 03:35
  • yet i have another relevent question how can i catch the pid of at command currently execute task to write to a file – mlzboy Sep 23 '10 at 11:22
  • This answer is wrong. It will execute the file **now**, see @bstpierre answer with correct usage. – axil Aug 06 '15 at 09:32