Testing Crontab on Debian Ubuntu

。_饼干妹妹 提交于 2019-12-03 15:47:09

Ok, let's start over again.

Create a file, say cron.txt, with exactly the following contents (1 line):

* * * * * touch $HOME/CRON_IS_RUNNING

(Do not create CRON_IS_RUNNING manually.) Run

crontab cron.txt

which should quietly produce no output, then

crontab -l

which should print

* * * * * touch $HOME/CRON_IS_RUNNING

Wait a minute or so, perhaps 2 minutes, then

ls -l $HOME/CRON_IS_RUNNING

which should print something like

-rw-r--r-- 1 yourname yourgroup 0 2011-08-23 20:11 CRON_IS_RUNNING

If this all works, it will confirm that you can run cron jobs.

If that's successful, the problem may be with your test.py command. Does it work when you run it from the command line? If it works from the command line but not from cron, test.py might have some dependency on environment variables (cron jobs run with fewer environment variables set than interactive commands typically do).

The syntax in your wall command is wrong. To output a direct message you need to use the syntax: echo test | wall . wall test will look for a file named test.

Any cron messages will be in /var/log/syslog

Your cronjob will be located in /var/spool/cron/crontabs/username which is owned by

username:crontab

I ran a test on my system "Debian Squeeze" with your crontab slightly modified as the root user.

I created a file test.py with:

#!/usr/bin/python
print "hello"

created the wall file test with:

this is a test

Then ran crontab -e and added this to my crontab:

MAILTO=myemail@gmail.com
* * * * * wall /usr/local/src/test
* * * * * /usr/bin/python2.6 /usr/local/src/test.py > /usr/local/src/test.log

When it ran this was output to the screen:

Broadcast Message from root@X-Wing                                             
        (somewhere) at 21:09 ...                                               

This is a test   

The contents of test.log contained:

hello

This was added to /var/log/syslog:

Aug 23 21:08:49 x-wing crontab[9850]: (root) REPLACE (root)
Aug 23 21:08:49 x-wing crontab[9850]: (root) END EDIT (root)
Aug 23 21:09:01 x-wing /usr/sbin/cron[1615]: (root) RELOAD (crontabs/root)
Aug 23 21:09:02 x-wing /USR/SBIN/CRON[9860]: (root) CMD (/usr/bin/python2.6 /usr/local/src/test.py > /usr/local/src/test.log)
Aug 23 21:09:02 x-wing /USR/SBIN/CRON[9862]: (root) CMD (wall /usr/local/src/test)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!