I have wrote a small script to check if openvpn is running and start it if it's not.
Here is the script i'm running
#!/bin/bash **-x**
ps auxw | grep openvpn | grep -v grep > /dev/null
if [ $? != 0 ]
then
/etc/init.d/openvpn start > /dev/null
log="/root/ServerRestart.log"
echo "The Openvpn Server was restarted at\n" > $log
date >> $log
fi
here is the crontab:
* * * * * /root/vpnmonitor.sh
it shows in the syslog that it runs the script but it does not seem to actually execute, the script works fine when run from a terminal.
The openvpn service won't start whitout the rigth path.
Try to include on your "vpnmonitor.sh":
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
Like:
#!/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games
logger "VPN restarted from cron"
/etc/init.d/openvpn restart vpn-servername
I made a new cronjob in /etc/crontab rather than using crontab -e and it works now, thanks everyone.
Related
I wrote a script to check for some services if they are active or not and restart the services.
The script works when ran manually but when added to crontab it does not work, when I check the logs I get
/bin/sh: 1: Syntax error: end of file unexpected
This is my script
#!/bin/bash
echo "Checking odoo services"
sudo service odoo13 status | grep 'active (running)' > /dev/null 2>&1
if [ $? != 0 ]
then
echo "Odoo not running restarting..."
sudo service odoo13 restart > /dev/null
fi
echo "Checking nginx services"
sudo service nginx status | grep 'active (running)' > /dev/null 2>&1
if [ $? != 0 ]
then
echo "Nginx inactive restarting"
sudo service nginx restart > /dev/null
fi
I set the crontab to run every minute
*/1 * * * * /opt/check_services.sh > /dev/null 2>
I do not know, what seems to be the issue?
So I figured it out
instead of #!/bin/bash I used #!/usr/bin/env bash
strange isn't it?
Debian 8.6. No root.
I can use cron.
I need to check if application ( php ./somescript & ) running in background stopped, and restart it. How can I check it using bash?
Of course, there is ps aux | grep ....., but how do I automate it?
I suggest to take a look at keyword #reboot from man 5 crontab to start a job once at server startup.
One way to go about it would be:
Cron:
* * * * * env DISPLAY=:0 /folder/myscript >/dev/null 2>&1
The env DISPLAY=:0 might not be needed in your case, or it might be needed, depending on your script (note: you might need to adapt this to your case, run echo $DISPLAY to find out your variable on the case).
Script:
#!/bin/bash
testvar="$(ps aux | grep -s "somescript" | grep -sv "grep")"
if [ -z "$testvar" ]; then nohup /folder/somescript &; fi
exit 0
This all could and should be fine tuned to your needs, but I believe this example could serve you well.
Edit: I fixed a small oversight on the code (I added | grep -sv "grep" to get rid of the own grep process of looking for the file from the tesvar results).
I have a script that checks if MySQL service is running on my Linux server.
If I run the script manually it works fine, but when crontab runs the script it gets different results..
This is my script:
#! /bin/sh
TODAY=$(/bin/date)
UP=$(/sbin/service mysql status| /bin/grep 'SUCCESS' | /usr/bin/wc -l);
if [ "$UP" -ne 1 ];
then
echo "mysql not working, Date: $TODAY" >> /scripts/sql_log.txt;
sudo /bin/mail -s "MySql is DOWN" mail#mail.com < /dev/null
sudo /sbin/service mysql start
else
echo "mysql is working, Date: $TODAY" >> /scripts/sql_log.txt;
fi
I am using the full path of the commands..the only part that I do not understand 100% is:
if [ "$UP" -ne 1 ];
What is this -ne 1?
So in this case MySQL is running:
If I run the script manually it writes that MySQL is working in the log file.
But Crontab just write that MySQL is not running in the log file (even if it is running) and it does not send any mail or something
If mysql service is stopped and I run the script manually, it send me an email and start the service as it should...
Any idea?
Now it works..looks like that the problem was caused because I did not write the full path of the command..This is the script that I am using now and it is working:
#! /bin/sh
UP=$(/sbin/service mysql status| /bin/grep 'SUCCESS' | /usr/bin/wc -l);
if [ "$UP" -ne 1 ];
then
sudo /bin/mail -s "MySql is DOWN" mail#mail.com < /dev/null
sudo /sbin/service mysql start
fi
MAILTO=""
*/10 * * * * /bin/bash /var/www/sym_monitor/sym_start.sh > /var/www/migrate/root_start.txt 2>&1
*/10 * * * * /bin/bash /var/www/sym_monitor/stop.sh > /var/www/migrate/root_stop.txt 2>&1
Both these are jobs inside cron running at 10 min interval #17:30 second one starting and 1735 first one starting avoiding the killing of first job by second before it actually started.
First script consist of the following code
#!/bin/bash
value=$(</var/www/sym_monitor/man.txt)
if [ "$value" == "true" ]; then
ps -ef|grep sym |grep -v grep |awk '{ print $2 }'|sudo xargs kill -9;
fi
Second script consists of the following code.
#!/bin/bash
value=$(</var/www/sym_monitor/man.txt)
if [ "$value" == "true" ]; then
sleep 30;
cd /var/www/symmetric-ds-3.1.6/bin;
(sudo ./sym --port 8082 --server);
fi
The problem is when I run both the scripts unfortunately sym_start.sh is not executing. But when I remove the stop.sh and manually run the stop script then the only script in the cron is executing properly. why thus this happen? any idea?
can you try changing
(sudo ./sym --port 8082 --server);
to its absolute path
(sudo /var/www/symmetric-ds-3.1.6/bin/sym --port 8082 --server);
I think the path is not getting changed in the shell
This
#!/bin/bash
if [ `ps -ef | grep "91.34.124.35" | grep -v grep | wc -l` -eq 0 ]; then sh home/asfd.sh; fi
or this?
ps -ef | grep "91\.34\.124\.35" | grep -v grep > /dev/null
if [ "$?" -ne "0" ]
then
sh home/asfd.sh
else
echo "Process is running fine"
fi
Hello, how can I write a shell script that looks in running processes and if there isn't a process name CONTAINING 91.34.124.35 then execute a file in a certain place and I want to make this run every 30 seconds in a continuous loop, I think there was a sleep command.
you can't use cron since on the implementation I know the smallest unit is one minute. You can use sleep but then your process will always be running (with cron it will started every time).
To use sleep just
while true ; do
if ! pgrep -f '91\.34\.124\.35' > /dev/null ; then
sh /home/asfd.sh
fi
sleep 30
done
If your pgrep has the option -q to suppress output (as on BSD) you can also use pgrep -q without redirecting the output to /dev/null
First of all, you should be able to reduce your script to simply
if ! pgrep "91\.34\.124\.35" > /dev/null; then ./your_script.sh; fi
To run this every 30 seconds via cron (because cron only runs every minute) you need 2 entries - one to run the command, another to delay for 30 seconds before running the same command again. For example:
* * * * * root if ! pgrep "91\.34\.124\.35" > /dev/null; then ./your_script.sh; fi
* * * * * root sleep 30; if ! pgrep "91\.34\.124\.35" > /dev/null; then ./your_script.sh; fi
To make this cleaner, you might be able to first store the command in a variable and use it for both entries. (I haven't tested this).
CHECK_COMMAND="if ! pgrep '91\.34\.124\.35' > /dev/null; then ./your_script.sh; fi"
* * * * * root eval "$CHECK_COMMAND"
* * * * * root sleep 30; eval "$CHECK_COMMAND"
p.s. The above assumes you're adding that to /etc/crontab. To use it in a user's crontab (crontab -e) simply leave out the username (root) before the command.
I would suggest using watch:
watch -n 30 launch_my_script_if_process_is_dead.sh
Either way is fine, you can save it in a .sh file and add it to the crontab to run every 30 seconds. Let me know if you want to know how to use crontab.
Try this:
if ps -ef | grep "91\.34\.124\.35" | grep -v grep > /dev/null
then
sh home/asfd.sh
else
echo "Process is running fine"
fi
No need to use test. if itself will examine the exit code.
You can save your script in file name, myscript.sh
then you can run your script through cron,
*/30 * * * * /full/path/for/myscript.sh
or you can use while
# cat script1.sh
#!/bin/bash
while true; do /bin/sh /full/path/for/myscript.sh ; sleep 30; done &
# ./script1.sh
Thanks.
I have found deamonizing critical scripts very effective.
http://cr.yp.to/daemontools.html
You can use monit for this task. See docu. It is available on most linux distributions and has a straightforward config. Find some examples in this post
For your app it will look something like
check process myprocessname
matching "91\.34\.124\.35"
start program = "/home/asfd.sh"
stop program = "/home/dfsa.sh"
If monit is not available on your platform you can use supervisord.
I also found this question very similar Repeat command automatically in Linux. It suggests to use watch.
Use cron for the "loop every 30 seconds" part.