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

3
  • If the way you solved this is substantially different from the answers here, please add your own answer, or accept the answer that solved it. In any case, please don't add "Solved" and the solution to the question.
    – muru
    Oct 11, 2014 at 3:15
  • Muru, this answer has been resolved already.
    – DankyNanky
    Oct 11, 2014 at 3:42
  • "In any case, please don't add "Solved" and the solution to the question."
    – muru
    Oct 11, 2014 at 3:50

2 Answers 2

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.

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
9
  • Thank you for your assistance. I have removed the # from my commands (as I decided to add them in the event it helped). When running the grep on cron I note it is running as root (which is why I tried the second method). I have looked for the file in which the guide referenced but alas was not able to find it. I will re-read the guide.
    – DankyNanky
    Oct 10, 2014 at 12:04
  • cron runs as root because it is a system process, but that process will launch user jobs on their behalf as long as those users are permitted to run cron jobs in the first place.
    – MGodby
    Oct 10, 2014 at 12:05
  • Yes, I have looked for both the deny and allow options, none are present. Oddly enough the cron has appeared to have run, so now it will be matter of me tracking down what change worked. Thank you for the help.
    – DankyNanky
    Oct 10, 2014 at 12:09
  • Did you change the actual command portion from michael home/Documents/example.sh to /home/michael/Documents/example.sh directly before it began working?
    – MGodby
    Oct 10, 2014 at 12:11
  • 1
    @MGodby I believe sudo is rarely needed to view /var/log/syslog, since users doing so are usually already in the adm group (which confers access to syslog and some other potentially sensitive log files). adm doesn't confer administrative powers (the sudo group does that), not even on older version of Ubuntu (the admin group did that), but administrators on Ubuntu are usually members of both sudo and adm. Oct 10, 2014 at 17:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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