I'm trying to get a script to run according to a crontab entry. The script I have works fine in the terminal but will not run automatically as per the cron entry. The script is simply to create an empty file in the /testexport1 directory once an hour.

I used crontab -e to edit the crontab, which looks like this:

30 * * * * /bin/bash/ /testexport1/./createfilescript.sh

The script itself looks like this:

[root@centostest testexport1]# cat createfilescript.sh
#!/bin/bash

PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin:/testexport1

today="$( date +"%Y%m%d" )"
number=0

while test -e "$today$suffix.txt"; do
    (( ++number ))
    suffix="$( printf -- '-%02d' "$number" )"
done

fname="$today$suffix.txt"

printf 'Will use "%s" as filename\n' "$fname"
touch "$fname"

I added the PATH part to the top of the script to specify where the script was being run from (as per another article I have read).

Any ideas why this crontab entry does not seem to be running the script? Simple fix I'm sure but I'm going around in circles at the mo.

up vote 2 down vote accepted

The crontab is not running the script because /bin/bash/ can most likely not be found. This should read /bin/bash instead (note the lack of / at the end), or whatever the correct path is to bash on your system.

Also make sure that all utilities that you are using in the script are actually found in the $PATH that you set. It's more common to modify the path rather than to overwrite it, as the system's path usually include directories where things like touch may be found.

  • Thanks, I tried that but no luck! 40 * * * * /bin/bash /testexport1/createfilescript.sh . I also checked which bash and got the output /usr/bin/bash. Should I then refer have #!/usr/bin/bash at the top of my script and also /usr/bin/bash in crontab ? (It doesn;t seem to work though!) – Ernie Mar 14 at 13:44
  • 1
    @Ernie Yes, the path to bash should be changed if bash installed in another location. If you specify a #!-line in the script, make sure that the script is also executable and don't use an explicit interpreter in the crontab. – Kusalananda Mar 14 at 13:50
  • For what I have asked this is the answer, thank you! There is an issue with the script that its not outputting the files to the expected location, but cron is working as expected now. Thank you for your help on this one. – Ernie Mar 14 at 14:19
  • @Ernie wen you set fname, prefix the name with the path that you'd like to use, e.g. fname="/some/path/$today$suffix.txt". Also use the same path in the file test in the while loop. – Kusalananda Mar 14 at 15:08

date and touch binaries are in /bin, which is not part of your $PATH.

I was going just to add a comment, but because I cannot yet...

Because you are executing it from cron, the cron does not know about the folder you want the file is created on, so just change to that directory at the beginning of the script to solve it:

[root@centostest testexport1]# cat createfilescript.sh
#!/bin/bash

PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin:/testexport1

cd  /testexport1

today="$( date +"%Y%m%d" )"
number=0

while test -e "$today$suffix.txt"; do
    (( ++number ))
    suffix="$( printf -- '-%02d' "$number" )"
done

fname="$today$suffix.txt"

printf 'Will use "%s" as filename\n' "$fname"
touch "$fname"

Other thing is the cron line:

30      *       *       *       *       /bin/bash/ /testexport1/./createfilescript.sh

if the script is at /testexport1/ change the cron line with this one

30      *       *       *       *       /bin/bash /testexport1/createfilescript.sh
  • Thanks @fvidalmolina - I've tried adding the /usr/bin before touch and date. I added the testexport1 to my PATH in the script file and also export PATH. Doesn't seem to be working still though....I've made the file executable also and changed the cron entry file as you suggested... – Ernie Mar 14 at 12:34
  • edited with the modifications done, now it is working for me, I have change the line indicated and the cron line too. – fvidalmolina Mar 14 at 12:49
  • No, never source from cron! The crontab is not a shell script. Correct the broken /bin/bash/ instead (should be /bin/bash or just bash). – Kusalananda Mar 14 at 13:08
  • changed the cron line, but anyway the solution works fine! – fvidalmolina Mar 14 at 13:57

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.