Searched everywhere but cannot run my own sample bash script with crontab

Issue

#!/usr/bin/env bash
#!/bin/bash 
#!/bin/sh
filename='/home/supersaiyan/sample1.sh'
for i in 1 2 3 4 5
do
  echo "String $i"
  x1+="String"$i
done
echo "#Added string is: $x1" >> $filename
echo "The following string has been added to the file: $x1"

Here’s the bash script I created which runs fine when using the bash command. It doesn’t work in crontab though.

SHELL=/bin/bash
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin
5 * * * * echo '#tanginamo cron tester' >> /home/supersaiyan/samplerz.sh
* * * * * /bin/bash /home/supersaiyan/sample1.sh

Here’s what I put in crontab. The first line is just for testing and is working fine so I’m really lost on why the second one isn’t working.

EDIT: Tried removing the SHELL and PATH lines to no avail
The .sh file is already modified as executable

Solution

Your output should be going to a different file than your actual script file.

You need to change :

filename='/home/supersaiyan/sample1.sh'

to:

filename='/home/supersaiyan/sample1.out'

…and run the script again.
You may want to review it’s contents past line 10 or so.
Enjoy! 🙂

Answered By – ewoj

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published