[SOLVED] bash cronjob script to kill openvpn and run client.ovpn
Linux - GeneralThis Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
bash cronjob script to kill openvpn and run client.ovpn
Hello,
I am trying to write a script to kill openvpn and and start a new openvpn instance every hour. the script runs fine if I run it in terminal but cant get crontab to work.
While you haven't specifically said what you mean by "can't get it to work," please note that scripts run from cron have no environment defined, so some specific issues could be:
the location of killall is unknown
the location of openvpn is unknown
When writing a script, best practice is to use absolute paths to all commands.
Alternatively, define the PATH variable in the script.
While you haven't specifically said what you mean by "can't get it to work," please note that scripts run from cron have no environment defined, so some specific issues could be:
the location of killall is unknown
the location of openvpn is unknown
When writing a script, best practice is to use absolute paths to all commands.
Alternatively, define the PATH variable in the script.
Yes!!! This is correct. Change all commands in your script to absolute paths. Or, define the PATH variable manually at the beginning of the script:
Code:
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin
Last edited by username_11011; 06-09-2018 at 04:46 PM.
Thank you everyone for the replies. Unfortunately still cannot get to to run. I have edited the paths, Script runs fine from bash so the updated defined paths are correct.
My etc/crontab with different parameters.
Code:
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
# For details see man 4 crontabs
# Example of job definition:
# .---------------- minute (0 - 59)
# | .------------- hour (0 - 23)
# | | .---------- day of month (1 - 31)
# | | | .------- month (1 - 12) OR jan,feb,mar,apr ...
# | | | | .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# | | | | |
# * * * * * user-name command to be executed
15 * * * * omar /home/omar/killallopenvpn.sh
@reboot omar /home/omar/killallopenvpn.sh
# m h dom mon dow user command
12 * * * * omar /home/omar/killallopenvpn.sh
15 * * * * /home/omar/killallopenvpn.sh
@reboot /home/omar/killallopenvpn.sh
When you test your script manually, you run it as omar-- right? You don't su to root? Or, run the script with sudo? Ex:
Code:
sudo /home/omar/killallopenvpn.sh
If not, I would suggest trying something simpler for cron to run. Possibly:
Code:
15 * * * * echo "text" > ~/out.txt
Obviously, "15" can be "12" or "16" or whatever time is coming up, so cron will run this right away. Then of course, restart your cron daemon to make the change:
Code:
killall crond
crond
After the time has passed when the job should run, check out.txt and verify cron wrote "text" to ~/out.txt. If this works, we know there's an issue with your script. If cron won't even echo (which is shell builtin), then something more bizarre is going on. If you *are* running your script as root or sudo when it works, you may need to tell cron to run it as root or sudo.
I'm sorry, I'm still not clear about "cannot get it to run"
Are you saying that cron is not executing the script?
Or that the script is not doing what you expect?
How do you know?
What does the cron log say? It should report if the job was executed. (mine is at /var/log/cron)
Please clarify which you think the problem is...
username_11011 has a good point about the user. In the crontab you've posted you are running the job at 12 with user omar, and at 15 with no user (so root, I think). Can a non-root user kill and restart openvpn? I don't know.
username_11011's idea about a test is also good, except the '~' means 'this user's home directory'...and there's no user, exactly. Suggest defining an absolute path there as well:
Code:
15 * * * * echo "text" > /home/omar/out.txt
Edit: Why do you need to kill and restart openvpn at boot?
Edit2: Is openvpn a service (guessing from your posted syntax re: cron)? Should it be?
When you test your script manually, you run it as omar-- right? You don't su to root? Or, run the script with sudo? Ex:
Code:
sudo /home/omar/killallopenvpn.sh
If not, I would suggest trying something simpler for cron to run. Possibly:
Code:
15 * * * * echo "text" > ~/out.txt
Obviously, "15" can be "12" or "16" or whatever time is coming up, so cron will run this right away. Then of course, restart your cron daemon to make the change:
Code:
killall crond
crond
After the time has passed when the job should run, check out.txt and verify cron wrote "text" to ~/out.txt. If this works, we know there's an issue with your script. If cron won't even echo (which is shell builtin), then something more bizarre is going on. If you *are* running your script as root or sudo when it works, you may need to tell cron to run it as root or sudo.
i run the script i created as root.
I tried to test echo but cron failed to create out.txt
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.