0

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"
1

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.

#!/bin/bash
PATH=/opt/someApp/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin

# rest of script follows

You can also set the PATH variable in the crontab file, which will apply to all cron jobs. E.g.

PATH=/opt/someApp/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin

15 1 * * * backupscript --incremental /home /root

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:

#!/bin/bash

Or better run your script as

0 0 * * * /bin/bash /path/to/script.sh

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:

 15 1 * * * ssh root@localhost "/path/to/script.sh"

The problem is with your script, though. Try using some bash script template this or this, which should eliminate any variable-assignment related problems.

  • shedbang is correct, and /bin/bash was never needed in my life but i will try it. i will post a complete answer later. im not at that company at the moment. I have also tried settingh path in script and in cron. no effect. full path to command should do the same. even shell builtin commands did not work if i want to assign the output to a variable. the command itself works when i t runs in the script but when i want to start a subshell and want to assign the output to varaible the varaible stays empty output through cron shows correct path */5 * * * * echo $PATH >> testfile.txt – Magnus Jan 14 '16 at 6:19
  • Bonus crunchtest: can you start is with screen -d -m /path/to/script.sh in your cron please? – Anubioz Jan 14 '16 at 8:19
  • Also, can you test global /etc/crontab, the one where you should write usernames before command like 0 0 * * * root /bin/bash /path/to/script.sh? Does it fail too that way? – Anubioz Jan 14 '16 at 8:31
  • there is no screen command. its a server without GUI i tried it in system crontab with username. it produce the same results. .commands worked except assigning Next i will try same script on different Server and distribution. – Magnus Jan 14 '16 at 9:53
  • GNU Screen has nothing to do with GUI, it's used for maintaining persistent terminal sessions. Can you please install it with this guide? – Anubioz Jan 14 '16 at 9:55

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.