LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This 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


Reply
  Search this Thread
Old 06-08-2018, 03:43 PM   #1
toxine
Member
 
Registered: May 2010
Distribution: Ubuntu/CentOs
Posts: 33

Rep: Reputation: 3
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.

my current settings:

Code:
#!/bin/sh
killall openvpn
openvpn /home/user/nyc.ovpn
things I have done:

Quote:
systemctl enable crond
Edited /etc/crontab
inserted:
0 */1 * * * /home/user/killallopenvpn.sh
chmod +x /home/user/killallopenvpn.sh
what am i doing wrong?
 
Old 06-08-2018, 04:06 PM   #2
username_11011
Member
 
Registered: Nov 2017
Location: Odessa, TX
Distribution: slackwarearm
Posts: 30

Rep: Reputation: Disabled
I'm trying to understand the "/1" part? This is a sample from *my* crontab file:
Code:
# Run hourly cron jobs at 47 minutes after the hour:
47 * * * * /usr/bin/run-parts /etc/cron.hourly 1> /dev/null
So, my assumption would be that all you need is "0 * * * *".
 
Old 06-08-2018, 04:10 PM   #3
toxine
Member
 
Registered: May 2010
Distribution: Ubuntu/CentOs
Posts: 33

Original Poster
Rep: Reputation: 3
Quote:
Originally Posted by username_11011 View Post
I'm trying to understand the "/1" part? This is a sample from *my* crontab file:
Code:
# Run hourly cron jobs at 47 minutes after the hour:
47 * * * * /usr/bin/run-parts /etc/cron.hourly 1> /dev/null
So, my assumption would be that all you need is "0 * * * *".
I have fixed it. I also included @reboot for testing but still will not run at reboot.
 
Old 06-08-2018, 04:26 PM   #4
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,696

Rep: Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894
Code:
* = */1
/etc/crontab is a system wide crontab which has an addition column for "run as user"

Code:
# m h dom mon dow user	command
  0 *  *   *   *  user  /home/user/killallopenvpn.sh
Replace user with the actual username.
 
1 members found this post helpful.
Old 06-08-2018, 04:36 PM   #5
username_11011
Member
 
Registered: Nov 2017
Location: Odessa, TX
Distribution: slackwarearm
Posts: 30

Rep: Reputation: Disabled
Also, make sure your cron daemon is running. I usually kill mine, first (otherwise, you could start multiple daemons):
Code:
killall crond
crond
 
Old 06-08-2018, 06:33 PM   #6
toxine
Member
 
Registered: May 2010
Distribution: Ubuntu/CentOs
Posts: 33

Original Poster
Rep: Reputation: 3
still cannot get it to work

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
45 * * * *	omar    /home/omar/killallopenvpn.sh
@reboot         omar    /home/omar/killallopenvpn.sh
# m h dom mon dow user  command
  28 *  *   *   *  omar  /home/omar/killallopenvpn.sh
 
Old 06-08-2018, 06:47 PM   #7
scasey
LQ Veteran
 
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: CentOS 7.9.2009
Posts: 5,727

Rep: Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211
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.
 
1 members found this post helpful.
Old 06-08-2018, 06:58 PM   #8
username_11011
Member
 
Registered: Nov 2017
Location: Odessa, TX
Distribution: slackwarearm
Posts: 30

Rep: Reputation: Disabled
Quote:
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.
 
Old 06-09-2018, 04:43 PM   #9
toxine
Member
 
Registered: May 2010
Distribution: Ubuntu/CentOs
Posts: 33

Original Poster
Rep: Reputation: 3
Hello,

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
my killallopenvpn.sh script
Code:
#!/bin/bash
/usr/bin/killall openvpn
/usr/sbin/openvpn --config /home/omar/nyc.ovpn
 
Old 06-09-2018, 05:08 PM   #10
username_11011
Member
 
Registered: Nov 2017
Location: Odessa, TX
Distribution: slackwarearm
Posts: 30

Rep: Reputation: Disabled
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.
 
Old 06-09-2018, 05:33 PM   #11
scasey
LQ Veteran
 
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: CentOS 7.9.2009
Posts: 5,727

Rep: Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211
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?

Last edited by scasey; 06-09-2018 at 06:05 PM.
 
Old 06-10-2018, 12:42 AM   #12
toxine
Member
 
Registered: May 2010
Distribution: Ubuntu/CentOs
Posts: 33

Original Poster
Rep: Reputation: 3
Quote:
Originally Posted by username_11011 View Post
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
 
Old 06-10-2018, 04:54 AM   #13
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,830

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308
Code:
15 * * * * echo "text" > ~/out.txt
probably echo not found too - use full path, and you ought to use explicit path instead of ~

Last edited by pan64; 06-10-2018 at 08:10 AM.
 
Old 06-10-2018, 06:39 AM   #14
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,696

Rep: Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894
What distribution/version are you running?

Have you checked the logs to see if the cron job was started?

If you added the echo test to /etc/crontab then you need to use the full path to out.txt and you still to specify a user.
 
1 members found this post helpful.
Old 06-10-2018, 08:34 AM   #15
toxine
Member
 
Registered: May 2010
Distribution: Ubuntu/CentOs
Posts: 33

Original Poster
Rep: Reputation: 3
Thanks everyone! got it to work.

Fixed it by temp disabling selinux. chmod +x the script. Defining absolute paths. running command as root if required (do not leave it blank).

Last edited by toxine; 06-10-2018 at 09:42 AM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] How to import *.ovpn files to NetworkManager-openvpn ? coralfang Slackware 6 03-01-2018 12:03 PM
No internet connection after .ovpn file run (OpenVPN) snovosel112811 Linux - Server 2 01-21-2017 08:51 AM
using .ovpn file on Gadmin-OpenVPN-Client LadyIris Linux - Networking 1 06-02-2015 10:04 PM
Ubuntu: Autoconnect OpenVPN client (GUI) THEN afterwards run a script (as root) riahc3 Linux - Networking 8 03-11-2013 03:34 AM
bash script: run multiple jobs in bg, & kill them separately? mattengland Programming 1 03-26-2006 05:07 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 02:56 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration