0

I have a shell script that writes the date to a log file when executed. When I run the script manually, the correct output gets written to the file. However, this needs to be automated, and when I run as a cron job, nothing is getting written to the file and I am confused why.

crontab:

0 * * * * tomcat /usr/bin/sh /apps/rdsreplication/snap_replication.sh

Sample Code:

#/bin/bash/

echo ---------------------------------------- >> create_snap.txt
echo Start time:  >> create_snap.txt
date >> create_snap.txt

Any help would be appreciated!

  • What working directory pwd does cron run things under? – thrig Mar 2 '17 at 17:07
  • 1. Do you actually have a /usr/bin/sh? This is an unusual location for a shell. 2. Which crontab file are you using, or how are you adding this line to cron? 3. What gets reported in cron's log file (typically somewhere in /var/log) for this job? – roaima Mar 2 '17 at 17:08
  • @thrig, this is the crontab for the tomcat user, so I assume it is running out of /home/tomcat/. – N.M.D Mar 2 '17 at 17:10
  • 2
    @thrig it's the user account's home directory – roaima Mar 2 '17 at 17:10
  • 1
    @roaima: many systemd-based distros have /bin as a symlink to /usr/bin nowadays, since it is no longer possible to have /usr on a separate partition anyway. – Ulrich Schwarz Mar 2 '17 at 17:19
3

The shell script needs to use the full path for the log file:

#/bin/bash/
# assuming you want the txt file in the same directory as the bash script
logfile="$(dirname "$0")/create_snap.txt"
{
    echo ----------------------------------------
    echo Start time:
    date 
} >> "$logfile"
0

For a user crontab, the man page for crontab(5) describes the fields:

Each line has five time and date fields, followed by a user name if this is the system crontab file, followed by a command.

So in your case, you'd want this:

0 * * * * /usr/bin/sh /apps/rdsreplication/snap_replication.sh

You should be aware that cron will email you the output and stderr from the job (if any), so you probably have quite a lot of emails telling you that cron can't find the command tomcat. (Or if it can, that tomcat doesn't understand the rest of the line.)

  • I tried this, but the date is still not getting written to the log file. I am assuming this is because it is running out of /home/tomcat/ and not /apps/rdsreplication/ where the log file resides? I think if I add this line to the script it should work: cd /apps/rdsreplication/. – N.M.D Mar 2 '17 at 17:18
  • 1
    @N.M.D I haven't seen your script or your system's setup so I can't comment on good locations to run the script from. The cron entry will be run from the user acccount's home directory, so if the script can't run from there it needs to change directory - or you need to use cd /correct/path && sh /apps/.../snap_replication.sh. – roaima Mar 2 '17 at 17:24

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.