Issue
I am not in the root, I entered the following commands in the crontab:
*/1 * * * * /home/ajain/testscript.sh
The file testscript.sh has the following commands:
#!/bin/bash
echo "The script begins now"
ping -c 2 live.com
echo The script has been run on `date` >> /home/ajain/testscript.log
echo "The script ends now"
exit
The crontab is not giving the results, however, the following command is giving the result in the testscript.log file correctly, displaying the ping date.
bash testscript.sh
Why is the crontab not working?
Solution
You can fix it in two different ways.
To provide full path to the script /home/ajain/testscript.sh. Here you don't even need to add
bash
because you have clearly mentioned in whichshell
your script should run i.e. first line of your script#!/bin/bash
Add this line before executing the script
set path=$path:/home/ajain/ testscript.sh # no need to use bash in front of it
Also providing execution permission to a script is not just enough. You need to check whether the user who is going to execute the script has permission to the location of the script or not. That means whether user can do a cd /home/ajain/
or not.
Hope this will help you.
Answered By - Abhijit Pritam Dutta