2

Okay, so I have had a quick look at the answers posted on this question and re-formulated my cron job. So, let me break this down as clear as I am able to:

PC/OS | Shell Info

I am currently running Lubuntu 14.04LTS x64 on my Toshiba Laptop. Basically I have (at present) a shell script that copies directory A, B, C, D, E, F and G to my Dropbox Directory, and a local copy. For all intents and purposes, let us call this example.sh which is stored in the folder /home/user/documents. I have read up on Cron Jobs, watched a few videos etc. and have had no luck with this.

To clarify, I have tested the script and it works. My first idea was the following:

Open a terminal and run crontab -e and manually add the following:

30 * * * * michael home/Documents/example.sh

and even

@hourly michael home/Documents/example.sh

I have tried as root, and as also removing the user part. This has not helped. Recently, I watched a video of where you go root and navigate to etc/cron.hourly/ and create the file, and change the ownership. I employed all of the above (followed the video exactly) and I was still not able to complete this process. Confused, I tried to re-do process 1, and instead of home/michael/Documents I pointed to the above location and have had no luck.

The frustrating point here is I am missing something incredibly simple (like, uh, this question I did!!).

2 Answers2

2

Creating your crontab file with crontab -e is probably the best method for you, but starting your crontab lines with the # character tells the system to ignore those lines. Remove the # and leading whitespace before the 30 in your crontab line. Should just look like this:

30 * * * * /home/michael/Documents/example.sh

Additionally, please ensure that cron is running ( ps -ef | grep cron ) and that your user is permitted to run cron jobs. If you are unaware of how to do this, reference the "Enable User Level Cron" section in the following link for specifics on how to enable cron on a user level: Ubuntu Community Help - CronHowTo

To see more about what cron is doing and why, run this command:

sudo grep -i cron /var/log/syslog | less
MGodby
  • 1,172
2

Some points that seem to be confusing you:

  • # is the comment character. Adding it to the beginning of a line makes cron ignore that line.
  • Don't mess with /etc/cron/hourly etc. There's no need to and they have a different format (see next point).

  • The format of a user's crontab is

    minute hour day-of-month month day-of-week command
    

    The format of the files under /etc/cron is different and also includes a user name:

    minute hour day-of-month month day-of-week user command
    
  • You need to give the full path. Paths in Linux always start with a /. That represents the root directory, the equivalent of Windows' C:/.

  • You need to make sure your script is executable. Run chmod +x /home/Documents/example.sh to make it so.

So, to run a script that is located at /home/Documents/example.sh (note the leading /) every 30 minutes, you would run crontab -e as yourself, not root, and add this line to it:

30 * * * * /home/Documents/example.sh

Save and close the crontab and you're set.

NOTE: In general, a useful troubleshooting trick for this kind of thing is to redirect the error output of your script to a file:

30 * * * * /home/Documents/example.sh 2> /tmp/mycrontab.log

That will create the file /tmp/mycrontab.log where any errors produced when attempting to run your script will be printed.

terdon
  • 104,119