0

So in the past I just did a simple shutdown crontab, and that was it. Lately, I want to do something a bit more complex, so I'm trying to get it to run a script. Right now I've boiled it down to a super test script, called test.sh. This script just echoes something to the console and to a file.

However, no matter how many times I try, it doesn't seem to be working. Here is the script:

#!/bin/bash

echo "Console foo!!!"
echo "Foo!!!" > ~/foo22.txt

And here is my crontab:

08 18 * * * /home/craig/Documents/Scripts/test.sh

I've double and triple checked the path to the file, it's good. The script is executable, I can run it manually from the terminal and it executes.

I'm doing this in the root crontab, ie "sudo crontab -e", if that helps at all.

  • please provide outputs of ls -l /home/craig/Documents/Scripts/test.sh and grep -i cron /var/log/syslog/. – geruetzel May 2 '16 at 22:15
  • use absolute file paths in your script. Any kind of "tricks" like "~" "$HOME" etc. will fail. – KWubbufetowicz May 2 '16 at 22:23
  • Running a user script as root would allow that user (or anyone who compromises that account) to gain root by altering what the script does. Other problems might include NFS or home directory encryption which may prevent root from accessing that directory. – thrig May 2 '16 at 23:09
  • 1
    @KWubbufetowicz Cron jobs run with very few environment variables, but HOME is one of the few that are defined. Using ~ or "$HOME" is fine. – Gilles May 3 '16 at 22:51
2

Applications run from cron have no "console". Both stdout and stderr are captured and emailed to the local user account when the job completes.

In your case the script is run as root, so the result will in in root's email. As for writing to the file, the ~ represents the root user's home directory, i.e. /root, so that is where you need to look for the output file.

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.