Starting pppd from cron doesn't work - linux

I want to start pppd whenever it disconnects. I am trying to setup a shell script to run every 1 minute to see if it's down and reconnect.
I have a bash script called vpn-check.sh:
ping -c3 10.8.3.0 > pingreport
result=`grep "0 received" pingreport`
truncresult="`echo "$result" | sed 's/^\(.................................\).*$/\1/'`"
if [[ $truncresult == "3 packets transmitted, 0 received" ]]; then
pon VPNname
fi
When I run this script from cli directly, it works and starts ppp but when I run the same through cronjob (for root user), it doesn't work.
I tried the below and didn't work
*/1 * * * * bash /root/vpn-check.sh > /root/cronlog.txt 2>&1
I tried the below and didn't work
*/1 * * * * /root/vpn-check.sh > /root/cronlog.txt 2>&1
Finally, I tried:
*/1 * * * * /usr/sbin/pppd call VPNname> /root/cronlog.txt 2>&1
Can't figure out what could be wrong.

I still don't understand why some scripts work and don't work from cron when it is being called as the correct user according to the logs.
The only solution I found is to run:
crontab -e
and add the following lines to the top (even though I am calling the pppd daemon by the full path):
SHELL=/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin

I fixed it. All this while I was running crontab -e
but for the user name to be added, it needs to be added to the system-wide cron file found under /etc/crontab
user that starts the job can only be added in the above mentioned system wide cron file.

You are missing the shebang from your shell script.
vpn-check.sh should look like:
#!/bin/bash
ping -c3 10.8.3.0 > pingreport
result=`grep "0 received" pingreport`
truncresult="`echo "$result" | sed 's/^\(.................................\).*$/\1/'`"
if [[ $truncresult == "3 packets transmitted, 0 received" ]]; then
pon VPNname
fi
See:
http://linuxconfig.org/bash-scripting-tutorial
What does the line "#!/bin/sh" mean in a UNIX shell script?

I was getting the same problem too.I used #dave's answer to figure it out. You just need to add the user name to the crontab, Add the next line to the end of /etc/crontab file:
*/1 * * * * root bash /root/vpn.sh
Replace the shell name of your own.

Related

Where Are Exit Codes For Cron Jobs Stored In Centos / Red Hat Servers?

I just need to know:
Our systems are set up to use /var/log/cron to store cron logs, however, there are no exit codes stored here just start/stop times and the PID of the jobs. How can I force our servers to just log a simple exit code for the cron job?
Note: I'm not trying to force the script that is being called to report its own exit code I just wanted to see if it was possible that Cron itself could log is own exit status. Almost if you were able to do an echo $? right after it ran to see what the code was.
you should be able to get the return value by referring to $? - a users crontab (which is not allowed to write to /root/no-no) it looks like this:
24 15 * * * /bin/echo "foo"; logger \`echo $?\`
25 15 * * * /bin/echo "bar" > /root/no-no; logger \`echo $?\`
then see the output of logger in this case.
I think you are lookin for,
/60 * * * * /path/to/Script >/path/to/log 2>&1; [ $? != 0 ] && mail -s "SUBJECT" -E email#example.com < /path/to/log
This will notify you when exit code is not equal to 0 or when cron job will fail.

Crontab not giving results

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?
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 which shell 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.
remove >> /home/ajain/script.log, just add line to the top of file /home/ajain/testscript.sh:
#!/bin/bash
exec >> /home/ajain/script.log 2>&1
echo "The script begins now"
ping -c 2 live.com
echo "The script has been run on `date`"
echo "The script ends now"
exit
remove >> /home/ajain/script.log from crontab, just using:
*/1 * * * * /home/ajain/testscript.sh
You need do define script output.
Because of cron man page:
When executing commands, any output is mailed to the owner of the
crontab (or to the user named in the MAILTO environment variable in
the crontab, if such exists). The children copies of cron running
these processes have their name coerced to uppercase, as will be seen
in the syslog and ps output.
Fore everything else than echo The script has been run on 'date' >> /home/ajain/testscript.log you should check your/root's mail, or the syslog (eg. /var/log/syslog).
I could recommend make something like this:
*/1 * * * * /home/ajain/testscript.sh >> /home/ajain/script.log
#Edit
My crontab file on my user:
$crontab -l
# test
*/1 * * * * /home/3sky/tests/test.sh >> /home/3sky/tests/log.log
Script look like that:
#!/bin/bash
echo "The script begins now"
ping -c 2 live.com
echo The script has been run on `date`
echo "The script ends now"
exit
Permission on files:
test.sh - 0755/-rwxr-xr-x
log.log - 0644/-rw-r--r--
Output in log file:
$tail -20 log.log
2 packets transmitted, 0 received, 100% packet loss, time 10999ms
The script has been run on Thu Jun 21 12:18:12 CEST 2018
The script ends now
The script begins now
PING live.com (204.79.197.212) 56(84) bytes of data.
--- live.com ping statistics ---
2 packets transmitted, 0 received, 100% packet loss, time 10999ms
The script has been run on Thu Jun 21 12:19:12 CEST 2018
The script ends now
The script begins now
PING live.com (204.79.197.212) 56(84) bytes of data.
--- live.com ping statistics ---
2 packets transmitted, 0 received, 100% packet loss, time 10999ms
The script has been run on Thu Jun 21 12:20:12 CEST 2018
The script ends now

Cron not executing the shell script + Linux [duplicate]

I have a script that checks if the PPTP VPN is running, and if not it reconnects the PPTP VPN. When I run the script manually it executes fine, but when I make a cron job, it's not running.
* * * * * /bin/bash /var/scripts/vpn-check.sh
Here is the script:
#!/bin/sh
/bin/ping -c3 192.168.17.27 > /tmp/pingreport
result=`grep "0 received" /tmp/pingreport`
truncresult="`echo "$result" | sed 's/^\(.................................\).*$$'`"
if [[ $truncresult == "3 packets transmitted, 0 received" ]]; then
/usr/sbin/pppd call home
fi
finally i found a solution ... instead of entering the cronjob with
crontab -e
i needed to edit the crontab file directly
nano /etc/crontab
adding e.g. something like
*/5 * * * * root /bin/bash /var/scripts/vpn-check.sh
and its fine now!
Thank you all for your help ... hope my solution will help other people as well.
After a long time getting errors, I just did this:
SHELL=/bin/bash
PATH=/bin:/sbin:/usr/bin:/usr/sbin
* * * * * /bin/bash /home/joaovitordeon/Documentos/test.sh
Where test.sh contains:
#!/bin/bash
/usr/bin/python3 /home/joaovitordeon/Documentos/test.py;
In my case, the issue was that the script wasn't marked as executable. To make sure it is, run the following command:
chmod +x your_script.sh
If you're positive the script runs outside of cron, execute
printf "SHELL=$SHELL\nPATH=$PATH\n* * * * * /bin/bash /var/scripts/vpn-check.sh\n"
Do crontab -e for whichever crontab you're using and replace it with output of the above command. This should mirror most of your environment in case there is some missing path issue or something else. Also check logs for any errors it's getting.
Though it definitly looks like the script has an error or you messed something up when copying it here
sed: -e expression #1, char 44: unterminated `s' command
./bad.sh: 5: ./bad.sh: [[: not found
Simple alternate script
#!/bin/bash
if [[ $(ping -c3 192.168.17.27) == *"0 received"* ]]; then
/usr/sbin/pppd call home
fi
Your script can be corrected and simplified like this:
#!/bin/sh
log=/tmp/vpn-check.log
{ date; ping -c3 192.168.17.27; } > $log
if grep -q '0 received' $log; then
/usr/sbin/pppd call home >>$log 2>&1
fi
Through our discussion in comments we confirmed that the script itself works, but pppd doesn't, when running from cron. This is because something must be different in an interactive shell like your terminal window, and in cron. This kind of problem is very common by the way.
The first thing to do is try to remember what configuration is necessary for pppd. I don't use it so I don't know. Maybe you need to set some environment variables? In which case most probably you set something in a startup file, like .bashrc, which is usually not used in a non-interactive shell, and would explain why pppd doesn't work.
The second thing is to check the logs of pppd. If you cannot find the logs easily, look into its man page, and it's configuration files, and try to find the logs, or how to make it log. Based on the logs, you should be able to find what is missing when running in cron, and resolve your problem.
Was having a similar problem that was resolved when a sh was put before the command in crontab
This did not work :
#reboot ~myhome/mycommand >/tmp/logfile 2>&1
This did :
#reboot sh ~myhome/mycommand >/tmp/logfile 2>&1
my case:
crontab -e
then adding the line:
* * * * * ( cd /directory/of/script/ && /bin/sh /directory/of/script/scriptItself.sh )
in fact, if I added "root" as per the user, it thought "root" was a command, and it didn't work.
As a complement of other's answers, didn't you forget the username in your crontab script ?
Try this :
* * * * * root /bin/bash /var/scripts/vpn-check.sh
EDIT
Here is a patch of your code
#!/bin/sh
/bin/ping -c3 192.168.17.27 > /tmp/pingreport
result=`grep "0 received" /tmp/pingreport`
truncresult=`echo "$result" | /bin/sed 's/^\(.................................\).*$/\1/'`
if [[ $truncresult == "3 packets transmitted, 0 received" ]]; then
/usr/sbin/pppd call home
fi
In my case, it could be solved by using this:
* * * * * root ( cd /directory/of/script/ && /directory/of/script/scriptItself.sh )
I used some ./folder/-references in the script, which didn't work.
The problem statement is script is getting executed when run manually in the shell but when run through cron, it gives "java: command not found" error -
Please try below 2 options and it should fix the issue -
Ensure the script is executable .If it's not, execute below -
chmod a+x your_script_name.sh
The cron job doesn’t run with the same user with which you are executing the script manually - so it doesn't have access to the same $PATH variable as your user which means it can't locate the Java executable to execute the commands in the script. We should first fetch the value of PATH variable as below and then set it(export) in the script -
echo $PATH can be used to fetch the value of PATH variable.
and your script can be modified as below - Please see second line starting with export
#!/bin/sh
export PATH=<provide the value of echo $PATH>
/bin/ping -c3 192.168.17.27 > /tmp/pingreport
result=`grep "0 received" /tmp/pingreport`
truncresult="`echo "$result" | sed 's/^\(.................................\).*$$'`"
if [[ $truncresult == "3 packets transmitted, 0 received" ]]; then
/usr/sbin/pppd call home
fi
First of all, check if cron service is running. You know the first question of the IT helpdesk: "Is the PC plugged in?".
For me, this was happening because the cronjob was executing from /root directory but my shell script (a script to pull the latest code from GitHub and run the tests) were in a different directory. So, I had to edit my script to have a cd to my scripts folder. My debug steps were
Verified that my script run independent of cron job
Checked /var/log/cron to see if the cron jobs are running. Verified that the job is running at the intended time
Added an echo command to the script to log the start and end times to a file. Verified that those were getting logged but not the actual commands
Logged the result of pwd to the same file and saw that the commands were getting executed from /root
Tried adding a cd to my script directory as the first line in the script. When the cron job kicked off this time, the script got executed just like in step 1.
it was timezone in my case. I scheduled cron with my local time but server has different timezone and it does not run at all. so make sure your server has same time by date cmd
first run command env > env.tmp
then run cat env.tmp
copy PATH=.................. Full line and paste into crontab -e, line before your cronjobs.
try this
/home/your site folder name/public_html/gistfile1.sh
set cron path like above

Run a script with arguments using crontab

I know this may have been answered earlier in various posts, but I've not been able to make this run myself.
I have a bash script (service.sh) that I would like to run every minute. It needs an argument to be passed (start in this case).
Using another script (test.sh) I am scheduling the cron expression for the above script:
echo "* * * * * /opt/service.sh start" > /opt/cronForSecops
crontab /opt/cronForSecops
I can see by using crontab -l that this is being set correctly as:
* * * * * /opt/service.sh start
However, the service.sh does not run, and I see no logs/files being created (which the service.sh file is supposed to do, when I run it normally).
Can anybody please guide me on where I am going wrong?
I was having this same issue where I using the following crontab:
0 23 * * * sudo -u myname /home/myname/bin/buildme.sh -f >> /home/myname/log.txt
And inside the bash script I was using this to get the -f option:
while getopts ":f" opt; do
case $opt in
f)
force_full=1
;;
\?)
echo "Invalid option: -$OPTARG" >&2
;;
esac
done
So I noticed that the option wasn't being honored when I ran this through cron for some reason. Well, adding /bin/bash to the cronjob fixed it right up. The new crontab is:
0 23 * * * sudo -u myname /bin/bash /home/myname/bin/buildme.sh -f >> /home/myname/log.txt
Hope it helps!
Try creating a simple wrapper script called /opt/start-service.sh with this content:
#!/bin/sh
/opt/service.sh start
and make sure it's executable then use
* * * * * /opt/start-service.sh
as the crontab entry

Crontab without crontab -e

I would like to add a crontab schedule by doing this in my server:
echo "30 * * * * /home/my/var/dir/to/script /var/etc/etc/etc/" > crontab -e
Is there a way to do this without going doing crontab -e and then typing in the command?
Could try
1)nano /etc/crontab (or any other editor, e.g. emacs)
2)echo "30 * * * * /home/my/var/dir/to/script /var/etc/etc/etc/" > /etc/crontab
3)or put the contents of this into a file, then do "file > /etc/crontab"
like root:
echo "30 * * * * /home/my/var/dir/to/script /var/etc/etc/etc/" > /var/spool/cron/crontabs/username
We have the following setup in production on RHEL:
- a custom software starting sh in init.d , which
- handles cron start , stop , reload
- writes cron tasks into separate tmp file and loads this file with crontab -e
I have been only maintaining it for several months but it works like a charm ...
The proper fix is probably to use a file as specified in https://stackoverflow.com/a/4421284/377927, but it is possible to use tee to append a line to the crontab by doing e.g.:
echo "* * * * * ls" | EDITOR="tee -a" crontab -e
tee -a will append stdin to the file it gets specfied, the EDITOR variable tells crontab to use tee -a as editor.
If you have the whole crontab in a text file, you can upload a whole crontab to replace the old crontab by doing:
cat <crontab_text_file> | crontab -
This will wipe out your old crontab. Using '-' allows you to use standard input into the crontab.

Resources