Cron job isn't writing to log file
7,615
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"
Related videos on Youtube
Author by
AndG
Updated on September 18, 2022Comments
-
AndG over 1 year
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!
-
thrig about 7 yearsWhat working directory
pwd
does cron run things under? -
roaima about 7 years1. Do you actually have a
/usr/bin/sh
? This is an unusual location for a shell. 2. Whichcrontab
file are you using, or how are you adding this line tocron
? 3. What gets reported incron
's log file (typically somewhere in/var/log
) for this job? -
roaima about 7 years@thrig it's the user account's home directory
-
AndG about 7 years@roaima yes, on this server I ran
which sh
, and that was the path returned. I am using the tomcat user's crontab, I am adding the line usingcrontab -e
./var/log/cron
shows this:Mar 1 18:06:01 sonar CROND[1585]: (tomcat) CMD (/usr/bin/sh /apps/rdsreplication/snap_replication.sh)
-
Ulrich Schwarz about 7 years@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. -
roaima about 7 years@UlrichSchwarz I was thinking of Solaris (not used it for years, though)
-
thrig about 7 years@roaima maybe. But what directory is that?
NULL
on account of Active Directory doing lord knows what? Flapping between what LDAP is providing and a local account, depending on the whims ofsssd
or such?
-
-
AndG about 7 yearsI 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/
. -
roaima about 7 years@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 usecd /correct/path && sh /apps/.../snap_replication.sh
.