1

I'm trying to execute my cron job with my user (user).

The cron job has no problem installing and the script works if I just execute it. It is executable and I have the bash flag to indicate it is a bash script.

I created the script by running crontab -e logged in as user.

For testing purposes I will run this script every min but will eventually change it to 10 mins.

my cronjob should run my script which pings googles server every 10mins and creates a log of it so I can know if my devices ever loses internet connection to track an issue. I have another script that moves the log file every day and then logs it with a date and then this script should create another log file. currently it isn't making a file with this cronjob or updating the file once I run the script.

my cronjob

*/1 * * * * bash /home/user/ping_scripts/ping-test.sh

my script

#!/bin/bash

if [ "$(id -u)" != "0" ]; then
  exec sudo "$0" "$@"
fi

if [ ! -e /var/log/ping.log ];then
    touch /var/log/ping.log
fi

ping -q -c5 google.com > /dev/null

if [ $? -eq 0 ];then
     echo -e "[$(date)] \t\t google ping successful" >> /var/log/ping.log
else
    echo -e "[$(date)] \t\t google ping failed" >> /var/log/ping.log
fi

I have read a few other solutions on, but non had fixed my error.

(I cant describe how my post is different other than it is simply a question, one that I could not find the answer to while going through Why crontab scripts are not working? which is for unique errors to cron and the solution wasn't really unique to cron specifically. it had more to do with my script. To get the answer please simply refer to the checked off answer below.)

12

2 Answers 2

2

My problem was that my user was not a sudoer.

This problem can be solved by adding your user to /etc/sudoers file or by adding cron job as root.

sudo su
crontab -e

and placing cron job there

0

Try to give the cronjob as

* 1 * * * cd /home/user/ping_scripts/ && ./ping-test.sh

and also make sure that the script is executable

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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