0

I am curious about the following:

I have a bash script, which is executed once in a month through a cronjob. The following line is giving an error "unknown command" when ran through the cronjob:

echo $P | chpasswd

When I execute the bash script directly, it is working properly.

Anyone with an idea?

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Thommy Tomka
  • 332
  • 1
  • 3
  • 14
  • 1
    What is the PATH supplied to your `cron` job? Where is `chpasswd` stored? Since the directory where `chpasswd` is not listed in the path provided by `cron`, it fails to find it. You get a very limited environment with `cron`; anything the least out of the ordinary means great care is required, either setting PATH more fully or specifying the absolute pathname of the commands. – Jonathan Leffler May 03 '15 at 08:02
  • 1
    How do you set `P` for `echo` to echo it? Doesn't it set the same value each month? Is that wise? – Jonathan Leffler May 03 '15 at 08:03

1 Answers1

1

Converting commentary into an answer.

What is the PATH supplied to your cron job? Where is chpasswd stored? Since the directory where chpasswd is stored is not listed in the path provided by cron, it fails to find it. You get a very limited environment with cron; running anything the least out of the ordinary means great care is required.

Either set PATH more fully in the script run by the cron job, or specify the absolute pathname of the commands that are not in /bin or /usr/bin.

Incidentally, how do you set P for echo to echo it? Doesn't it set the same value each month? Is that wise?

There are numerous other questions on Stack Overflow about difficulties running commands from cron jobs. Amongst others, see Bash script not running in cron correctly and Perl script works but not via cron and Is there a special restriction on commands executed by cron?, to name but three.

Community
  • 1
  • 1
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • Thank You. My chpasswd is located @ /usr/sbin - I added the absolute path and now it is working when called via the cronjob. P is set different every month. There are fixed parts and varying parts, which in combination are P. The varying parts are calculated in a way that only I know and thus I know about the valid password at every time. – Thommy Tomka May 04 '15 at 06:45