Yes, this should run fine, assuming when you say "it runs fine on the command line" you're literally pasting the same command from the crontab entry. Things to check:
- Is the cron daemon actually running? (Run
pidof cron
)
- If it is running, try restarting it. (Depends on your flavour of Unix, but something like
service cron restart
or /etc/init.d/cron restart
)
- If your script still doesn't run, check everything in
/var/log
for appropriate output - the specific file depends on your syslog configuration, but /var/log/messages
and /var/log/syslog
are good options if /var/log/cron
doesn't exist.
Note: You'll need to be root
to do most of these things - if you don't have access then you'll need to speak to the person who administers the machine.
Also, when you say you open a file in /tmp
, that's probably the most reliable thing you can do to ensure it's running. Right at the start of your script (before imports or anything) add this:
with open("/tmp/testfile", "a") as fd:
fd.write("I am running\n")
You can also check if your crontab is being correctly installed - it should be placed in something like /var/spool/cron/crontabs
(that's on Ubuntu Linux, it may differ on different Unix flavours). You should see a file in there with your username which should contain your entry.
Finally, if you get really desperate you could strace
your cron process to see what it's doing:
sudo strace -f -p `pidof cron`
You don't need to worry too much about the specifics, but it should be doing something every minute. If it's not then something's going quite wrong.
Cron runs commands from a special shell, separate from the user or root shells. This shell does not have access to the same PATH variables as users. Therefore, when running a script as a cron job there are two options:
A. Specify the full path for every command in the script (I.e. full path to aptitude- cron doesn't know where to look to find "apt-get")
B. Little trick I use- when writing the cron job line, even in the ROOT crontab, append sudo before the script path. This will trick cron into running the script from a root shell instead of the cron shell, which will give it access to all root's PATH variables.
Best Answer
Run your command using launchd instead. Use the property list files in
~/Library/LaunchAgents/
and/System/Library/LaunchAgents/
as inspiration. Documentation on developer.apple.com.To specify a launch interval of e.g. 30 seconds, use the following: