Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I installed it in a machine at work and it wouldn't do anything, no matter what I did. So now I have it on virtualbox at home and it also doesn't work. Here's what I'm trying to do:

Have a file on /home/john/Desktop called test.sh. Its contents:

#!/bin/bash
echo "Bing!" > /tmp/cronjob

Have done a chmod 777 test.sh and can run it. Listing it shows as rwx for users/group/global. Then I do a crontab -e and add the following line:

* * * * * john /home/john/Desktop/test.sh

But it doesn't work. The file is never written. I've also tried using a specific time (for example, it's 9 PM so I set the hour to 05 21 and wait) but it doesn't work as well. I've also tried without the user "john" in the crontab but no dice. I tried using crontab -e -u john too. Nothing works.

Doing a service cron status I get that it is started/running. I've tried restarting it as well.

What am I doing wrong?

Also, if I need to run some commands as root I'm supposed to use it with crontab -e -u <username> right?

share|improve this question
add comment

3 Answers

There are three different ways to use cron.

  1. Putting scripts in the /etc/cron.hourly , /etc/cron.daily , /etc/cron.weekly or /etc/cron.monthly folders
    The system will then run those at the scheduled time with root privileges
  2. Editing /etc/crontab
    This takes a syntax like the one you were trying to use in crontab -e, because here you can specify which user should be responsible for the scheduled job, in your case, john.
  3. The crontab command, which saves a cron tab for the user that made the job in /var/spool/cron/crontabs

If you are using the third method, you don't need to tell it you are john, it already knows. You can however tell it with the -u flag to save the job in someone elses cron tab.
Using the third method, you do not need to have the john in * * * * * john /home/john/Desktop/test.sh

share|improve this answer
    
There's also the /etc/cron.d you didn't mention. –  vaab Mar 21 '13 at 14:43
add comment
* * * * * john /home/john/Desktop/test.sh

The "john" here is wrong. Only the system crontab has a user name field; the per-user crontabs which you edit with crontab -e do not have it (the user is implicit). Just leave it out.

Also, if I need to run some commands as root I'm supposed to use it with crontab -e -u right?

No, if you want a cronjob to run with root rights just do

sudo crontab -e

(or put it into the system crontab, with user name "root").

share|improve this answer
    
system crontab? could you explain? would it be right to use crontab -e -u root? –  john p Jul 30 '10 at 0:41
1  
@john p: Yes, and no. If you've editing the files in /etc/cron.whatever, then YES, you need a user name. If you're logged in as john, and editing your default crontab with crontab -e, then NO, you don't need a username. –  Satanicpuppy Jul 30 '10 at 3:04
add comment

Just to be sure:

crontab -e

then

30 21 * * * /home/john/Desktop/test.sh

EDIT: I mean that you don't need no "john" beside the time section of cron and the path to the command.

share|improve this answer
add comment

protected by studiohack May 8 '11 at 3:24

Thank you for your interest in this question. Because it has attracted low-quality answers, posting an answer now requires 10 reputation on this site.

Would you like to answer one of these unanswered questions instead?

Not the answer you're looking for? Browse other questions tagged or ask your own question.