Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Testing Crontab on Debian Ubuntu

I will preface this by saying I am very new to command line programming with Debian Ubuntu...

I have been trying to set up a crontab list on a Debian Ubuntu server but have not been able to get it to work. Here is a sample:

[email protected]
* * * * * wall test
* * * * * /usr/bin/python2.6 /home/user/test.py > /home/user/clean_tmp_dir.log

The above shows up when I type "crontab -l" but no resulting output appears in the console. The "test.py" is supposed to generate a csv file but none is being created.

I am not receiving any output/error emails. I tried to find a log, but "var/log/cron" does not exist, nor does "etc/syslog.conf"...I tried to edit "etc/rsyslog.conf", but got "E212: Can't open file for writing"...I am logged in, however. Do I need some sort of special administrative privileges? Do I need to specify user or "root" or something?

Does anyone know what I'm doing wrong, how I can create/view a log, or how I can perform any other straightforward tests? Thanks!

like image 570
Jen Avatar asked Aug 15 '11 21:08

Jen


2 Answers

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).

like image 114
Keith Thompson Avatar answered Nov 10 '22 14:11

Keith Thompson


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:

[email protected]
* * * * * 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)
like image 40
Chris_O Avatar answered Nov 10 '22 14:11

Chris_O