3

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?

3 Answers 3

2

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.

2

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

1
  • There's also the /etc/cron.d you didn't mention.
    – vaab
    Mar 21, 2013 at 14:43
1
* * * * * 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").

2
  • system crontab? could you explain? would it be right to use crontab -e -u root?
    – john p
    Jul 30, 2010 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. Jul 30, 2010 at 3:04

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .