script to start service from status check - bash

I have an app on Centos that daily shuts down. Until I can get the support on it, I would like to handle it from a script in order when this occur again. My current script looks like this:
#!/bin/bash
if /path/to/service_check status service | grep -q 'SHUTDOWN'; then
/path/to/service_check start service
fi
It is setup in crontab to run on one minute interval but it is not running correctly. Is this approach incorrect?
Thank you!

check crond services is running systemctl status crond
check your script`s privilage chmod +x scriptname.sh
execute the script manually on the terminal /path/to/your/scriptname.sh
check your crontab configure,you should set the user to run this script * * * * * root /path/to/your/scriptname.sh

Related

How to stop a service from a script called by a cron job?

I have a cron job that runs every X minutes and is supposed to restart the amazon-ssm-agent service. However, I noticed that my ruby script actually never does this.
Here's what my crontab looks like:
* * * * * /root/./my_script.rb
Here's the very plain contents of the ruby script:
#!/usr/bin/env ruby
#
command = "service amazon-ssm-agent stop"
system(command)
If I'm monitoring /var/log/amazon/ssm/amazon-ssm-agent.log, it never stops. If I check the service with service amazon-ssm-agent status, it still shows online.
What's the best way to stop or restart a service from within a ruby script that's called by Cron job? I have been facing a pretty major issue today and I think this is the reason why.
I have tried to capture the result and write it to a file, but when the cron job runs, the file is created but it's empty. Here's what the ruby script looks like now:
#!/usr/bin/env ruby
command = "service amazon-ssm-agent stop"
result = system(command)
File.open("/root/result.txt", "w") {|f| f.write(result)}
I have also tried to just modify the script to this:
#!/usr/bin/env ruby
command = "service amazon-ssm-agent stop"
but still no luck. I have even just modified my cron job itself to start the service rather than calling the ruby script that starts the service, but no luck:
* * * * * service amazon-ssm-agent start
The service never gets started.
Figured out the issue, which was silly. I realized that the service command doesn't reside in the environment paths that the cron job has, so that's why it never worked. Neither is ifconfig, which is what I was trying to for testing.
So I had to replace my service command in the Ruby script with /usr/sbin/service and it worked flawlessly afterwards.

How to restart a program in ubuntu using cron?

I'd like to restart a program regardless of it already running or not using crontab.
I have this in crontab -e
* * * * * /usr/bin/pkill -f myapp; /home/ubuntu/xyz/bin/res.sh
And in the res.sh
#!/bin/bash
/usr/bin/pkill -f myapp
sleep 10
/home/ubuntu/xyz/bin/myapp & &>/dev/null
The problem is if the program is running, it kills the program. but it fails to start the program again.
I don't want to use any condition as to check if the program is already running and start only if it's not.
EDIT: The program is not a service to use "/etc/init.d/service restart" or "sudo service myapp restart"
Thanks.

Start/stop a systemd service in cronjob

I have a service name bots in /etc/systemd/system/bots.service
I have a shell script name runcron.sh like this
service bots stop
service bots start
If I run ./runcron.sh the service will be stop then start successfully but if I put it in a crontab like this, it won't run successfully
*/5 * * * * /home/vps171-107/runcron.sh
How can I make runcron.sh stop then start the bots service in crontab ?
UPDATE
After the help from #armnotstrong , I've change the script to
/usr/sbin/service bots stop
/usr/sbin/service bots start
And it works!
It may be an env issue, crontab may execute the command with sh not bash try:
*/5 * * * * bash /home/vps171-107/runcron.sh

Magento Cron not working: no heartbeat task

I noticed the cron jobs not working because some tasks were not performed (automatic feed generation, google sitemap, ...).
1) I installed the (very useful) AOE scheduler
2) I've checked cron_schedule SQL table via PHPmyAdmin: no task is generated, but if I press Generate schedule in AOE scheduler, a list of task is generated. All tasks remain in pending status (executed at NULL).
3) I've set (crontab -e)
0,5,10,15,20,25,30,35,40,45,50,55 * * * * /home/fpl/webapps/magento/cron.sh
I've tried to run cron.sh manually via SSH. When I run manually the sh the heartbeat task is run. So I'm also sure the problem is not in the cron.sh script.
Cron is properly running on the server.
Configuration
Webfaction hosting
Magento ver. 1.8.0.0
Cron schedule Configuration on magento admin panel
Generate Schedules Every 1
Schedule Ahead for 20
Missed if Not Run Within 15
Heartbeat task schedule (cron syntax) 0,5,10,15,20,25,30,35,40,45,50,55 * * * *
Thanks for your help!
my guess is that your cron.sh is not executable and that's why nothing is happening.
Please check the file persmission and add the executable flag
chmod +x /home/fpl/webapps/magento/cron.sh
You might also want to check your server's log files for cron (e.g. https://askubuntu.com/questions/56683/where-is-the-cron-crontab-log)
Instead of relying on the fact that cron is executable you could also run it like this
/bin/bash /home/fpl/webapps/magento/cron.sh
And: instead of writing down the minutes like that you should be using this:
*/5 * * * * /bin/bash /home/fpl/webapps/magento/cron.sh
Did you check the status for crontab?
service crond status // depends on your OS
If its not running, start it
service crond start
And configure for system startup
chkconfig crond on
HTH
Good luck!
edit cron.sh and change line 39 to:
PHP_BIN=`/usr/local/bin/php56`
It tries to use which php but that isn't so great on webfaction servers as there are lots of php versions.
Adding
crontab -e */5 * * * * /usr/bin/wget -O /dev/null -q http://www.partsfortreadmill.com/cron.php
and setting a proper cron.php in the above path

Starting a Thin application via cron

I am running Shopify Dashboard on Centos 6 (http://shopify.github.io/dashing/). I wish to start this on boot and via a cron when I pull down an update from git.
I have the following code in a bash script which is the same code I run via the command line to start dashboard.
#!/bin/bash
cd /usr/share/dashboard/
dashing start -p 500 -d
running the actual script as the root user from the command line starts the application no problem.
However when this script is run via a cron or on boot then the application is never started.
If anyone could shed some light as to why this is the case it would most appreciated.
Per my comment I am still not 100% sure that the script is being run as root. I would add a line in the script:
echo $user > /tmp/test.txt
Then run the script via cron and see what the value of the file is.
Also I question your script. Why is it necessary to cd?
How about
/usr/share/dashboard/dashing start -p 500 -d
Also you may have to do a nohup, which is the no hang up signal, so ...
nohup /usr/share/dashboard/dashing start -p 500 -d
Those are my guesses.

Resources