1

I have two bash scripts. One runs as root, and it calls another one as user "parallels"

/root/cronrun.sh

#! /bin/bash

PARR="thisparameter"

echo "Starting at `date`" >> /root/rlog.log
runuser -l parallels -c "/home/parallels/testscript/newscript.sh $PARR"
echo "Finishing at `date`" >> /root/rlog.log

/home/parallels/testscript/newscript.sh

#! /bin/bash

PARAMM=$1

echo "`date` - newscript.sh ran with $PARAMM" >> /home/parallels/somelog.log

Ran /root/cronrun.sh from command line as root at

18:17:28 CET 
18:17:29 CET

Added to crontab

*/2 * * * * /root/cronrun.sh

So it ran at 18:20:00 CET via cron

After this:

/root/rlog.log

Starting at Thu Nov 16 18:17:28 CET 2017
Finishing at Thu Nov 16 18:17:28 CET 2017
Starting at Thu Nov 16 18:17:29 CET 2017
Finishing at Thu Nov 16 18:17:29 CET 2017
Starting at Thu Nov 16 18:20:01 CET 2017
Finishing at Thu Nov 16 18:20:01 CET 2017

/home/parallels/somelog.log

Thu Nov 16 18:17:28 CET 2017 - newscript.sh ran with thisparameter
Thu Nov 16 18:17:29 CET 2017 - newscript.sh ran with thisparameter

So the log entry from the echo in the runuser shell is missing. Why it can be possible? How does cron run differently in this case, which makes "runuser" command ignored/failed?

(System reproduced on is Ubuntu 16.04.3 LTS)

(SHELL=/bin/bash in crontab is not solving it)

  • for the first 2 runs yes (via command line). The 3rd run was via cron, and then it didn't run, only the line before and after – dszakal Nov 16 '17 at 17:38
  • and more importantly SHELL=/bin/bash adding to crontab is not solving it, just tried – dszakal Nov 16 '17 at 17:39
  • Could cron's $PATH not have runuser in it, while your interactive shell does? – Jeff Schaller Nov 16 '17 at 17:45
  • have the cronrun.sh script echo $PATH and also echo $? after the runuser command -- all to the rlog.log – Jeff Schaller Nov 16 '17 at 17:46
  • Please send this as an answer and I'll accept it Jeff - replacing it to /sbin/runuser solved it – dszakal Nov 16 '17 at 18:04
3

cron runs with a specific PATH, as seen in the upstream Debian source code:

#ifndef _PATH_DEFPATH
# define _PATH_DEFPATH "/usr/bin:/bin"
#endif

Referenced here:

#if defined(POSIX)
    setenv("PATH", _PATH_DEFPATH, 1);
#endif

and since runuser lives in /sbin, you'll need to use the full path to it, or set PATH in your script to include /sbin.

  • thanks (actually /sbin/runuser , but yes, this solved it ) – dszakal Nov 16 '17 at 18:22

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.