I know that my question was asked several times, but there is no solution for my problem.
In my Company there is an Application running on a RedHat Mashine. Users connect a text Based GUI via ssh to this Server.
This Application does not support Pasword aging resetting password.
I made a script checking password aging time and sending an eMail to each user.
The script is running fine from shell but not working in crontab.
Several tests (touch testfile) showed that crontab is working and all scripts are executed. I verified that with a simple echo command at the beginning of the new script.
deeper investigating with several echos showed me that there is a problem with assigning a command to a variable.
This worked for years and in several crontabs
VAR = `ls`
i tested other versions:
VAR = $(ls)
VAR = `/bin/ls`
VAR = $(/bin/ls)
Snippet from Script:
(... generating user/email array ...)
echo "----------------START "$LOGDATE" ---------------" >> $LOGFILE
for ((i=0; i<${#USER[*]} ; i++))
do
USERNAME=${USER[$i]}
EMPFAENGERNAME=${EMPFAENGER[$i]}
TEMP1=`chage -l $USERNAME | grep 'Kennwort läuft ab'|cut -d ":" -f2`
(... sending email to user telling him to change password ...)
Crontab: (root)
00 12 * * 1-5 /root/pw_warning.sh
That script is so simple and its working as root and with sudo, its nothing really complicated but i dont get the point what i am doing wrong.
Update:
Tested PATH=(..) in crontab and in script as suggested. No changes at all.
Again: crontab is working and executing scripts. Every command in the script is working for itself but not when it should be assigned to a variable.
Update2:
Adding path to shell did not work:
0 0 * * * /bin/bash /path/to/script.sh
Update3:
Working Workaround:
0 0 * * * ssh root@localhost "/path/to/script"
Best Answer
Cron passes a minimal set of environment variables to your jobs.
A common "gotcha" here is the PATH environment variable being different.
To get around that, just set your own PATH variable at the top of the script. E.g.
You can also set the PATH variable in the crontab file, which will apply to all cron jobs. E.g.
from here
Also, judging by variables assignments you're using, you script won't work in
/bin/sh
, make sure your first line is correct:Or better run your script as
instead of just
/path/to/script.sh
Finally, you may try to put the following hacky code in your crontab, since it should work exactly as if you had run the script from the shell yourself:
The problem is with your script, though. Try using some bash script template this or this, which should eliminate any variable-assignment related problems.