Shell script doesn't execute from cron job - linux

shell script:
#!/bin/sh
services=( httpd named proftpd mysqld dovecot postfix webmin)
for service in ${services[#]}
do
if ps ax | grep -v grep | grep $service > /dev/null
then
echo "$service service running, everything is fine"
else
echo "$service is not running"
service $service start
fi
done
file executable, running from root user
command:
bash /etc/mycron/checkServices.sh
tried sh and just /etc/mycron/checkServices.sh
doesn't run

#!/bin/sh
services=( httpd named proftpd mysqld dovecot postfix webmin)
for service in ${services[#]}; do
if ps ax | grep -v grep | grep $service > /dev/null; then
echo "$service service running, everything is fine";
else
echo "$service is not running";
service $service start;
fi;
done;
Works fine here... maybe you want to add after #!/bin/sh PATH="/bin:/sbin:/usr/bin:/usr/sbin:/opt/usr/bin:/opt/usr/sbin:/usr/local/bin:/usr/local/sbin"
You could also do chmod 775 /etc/mycron/checkServices.sh to make it executable, which is needed for cron. Then you would also not need to call bash /etc/mycron/checkServices.sh and can just call /etc/mycron/checkServices.sh the #!/bin/sh tells the executable loader to load the file with /bin/sh if you invoke bash /etc/mycron/checkServices.sh you will start bash which on his turn would start /bin/sh to finally execute your script.
Since the for loop in bash / sh uses the IFS variable ($IFS) as delimiter, you could also make the line services=(httpd named proftpd mysqld dovecot postfix webmin) as services="httpd named proftpd mysqld dovecot postfix webmin" since this is more general

Just as a general diagnostic process, it's sensible to insert trace statements into the script such as:
echo "Starting..."
echo "Checking for running service '$service'..."
which/whereis service
echo "Service has been started."
temporarily remove the > /dev/null for ps.
Then execute under cron with stdout and stderr redirected to a log file.
That allows you to identify the exact line that's failing. As others have said, it looks likely to be the "service" or "$service" commands aren't found because the PATH isn't set to include them. You do realise "service" itself is being sought as an external command that presumably launched $service in turn? Also keep an eye on root's mail, as cron sometimes sends error reports via mail.

Related

How to run script multiple times and after every execution of command to wait until the device is ready to execute again?

I have this bash script:
#!/bin/bash
rm /etc/stress.txt
cat /dev/smd10 | tee /etc/stress.txt &
for ((i=0; i< 1000; i++))
do
echo -e "\nRun number: $i\n"
#wait untill module restart and bee ready for next restart
dmesg | grep ERROR
echo -e 'AT+CFUN=1,1\r\n' > /dev/smd10
echo -e "\nADB device booted successfully\n"
done
I want to restart module 1000 times using this script.
Module is like android device witch has linux inside it. But I use Windows.
AT+CFUN=1,1 - reset
When I push script, after every restart I need a command which will wait module and start up again and execute script 1000 times. Then I do pull in .txt file and save all output content.
Which command should I use?
I try commands like wait, sleep, watch, adb wait-for-device, ps aux | grep... Nothing works.
Can someone help me with this?
I find the solution. This is how my script actually looks:
#!/bin/bash
cat /dev/smd10 &
TEST=$(cat /etc/output.txt)
RESTART_TIMES=1000
if [[ $TEST != $RESTART_TIMES ]]
then
echo $((TEST+1)) > /etc/output.txt
dmesg
echo -e 'AT+CFUN=1,1\r\n' > /dev/smd10
fi
These are the steps that you need to do:
adb push /path/to/your/script /etc/init.d
cd /etc
cat outputfile.txt - make an output file and write inside file 0 ( echo 0 > output.txt )
cd init.d
ls - you should see rc5.d
cd .. then cd rc5.d - go inside
ln -s ../init.d/yourscript.sh S99yourscript.sh
ls - you should see S99yourscript.sh
cd .. return to init.d directory
chmod +x yourscript.sh - add permision to your script
./yourscript.sh

Checking if a node.js app has started properly using bash

I wrote a node.js application and have written a bash script to start it and verify if it's running. I have my script run npm start & first, then I have a check to see if the ports I want open are open using netstat. The check works fine when I run it after the script is run, but during the running of the script, the check fails because the server has not fully started before the check is run. My code is below:
echo "Starting the server..."
npm start & > /dev/null 2>&1
if [[ -z $(sudo netstat -tulpn | grep :$portNum | grep node) ]] ; then
echo -e "\tPort $portNum is not in use, something went wrong. Exiting."
else
echo -e "\tPort $portNum is in use!"
fi
Is there a good way to take the above script and change it so that the check doesn't occur until the server is fully started? I don't want to use sleep if I can help it.
You can use a wait call:
echo "Starting the server..."
npm start & > /dev/null 2>&1
wait
if [[ -z $(sudo netstat -tulpn | grep :$portNum | grep node) ]] ; then
echo -e "\tPort $portNum is not in use, something went wrong. Exiting."
else
echo -e "\tPort $portNum is in use!"
fi
The only limitation to this is that if npm daemonizes itself then it's no longer a child of the script so the wait command will have no effect (the reason for this is that a process daemonizes itself by terminating and spawning a new process that inherits its role).

How to get the status of any service in a sh script on centos 7?

I am trying to get the service status (for a /bin/sh script) and start it if is not running.
I found some scripts, but does not work for centos 7.
There are, as usual, multiples ways to do this. Just one example, that check if postfix is running:
#!/bin/sh
PID=`cat /var/spool/postfix/pid/master.pid`
# echo $PID
PS=`/bin/ps axu | grep $PID | grep -v grep`
# echo $PS
if [ "$PS" = "" ]
then
/sbin/service postfix restart
fi
You could use "pid" file, or try to detect if process is running (parsing output of "ps axu |grep process_name"), or parse output of "service process status" command, etc, etc.

Cronjob not running bash script

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.

Bash: start remote python application through ssh and get its PID

I'm creating a little bash script to copy new files from a windows machine to a remote linux centos server (i run this script using the git-shell) then i want to restart the python application thats running in the server to use those new files.
The problem is that everytime i run this script i want to end the actual running process before i start it again, so i want to get the pid of the process i start and save it to a file in the remote host so i can read it from there the next time i run the program and kill it.
My code by now looks similar to this:
echo "Copying code files to server..."
# The destination folder has to exist in the server
scp -r ./python/ root#myserver:/root/
echo "Checking for running processes..."
if ssh root#myserver 'ls dmr.pid >/dev/null'; then
echo "PID file exists, reading file..."
PID=$(ssh root#myserver 'cat dmr.pid')
# Terminate the actual process
echo "Terminating the process with PID '$PID'..."
ssh root#myserver 'kill $PID'
else
echo "PID file doesn't exist, not known processes running"
fi
# Restart the server and get the PID
echo "Restarting the server..."
ssh root#myserver 'python /root/python/run_dev_server.py > /dev/null 2>&1 &'
SERV_PID=$(ssh root#myserver 'echo $!')
echo "Saving PID to file dmr.pid"
ssh root#myserver "echo '$SERV_PID' > \"dmr.pid\""
echo "Sucesfully finished!"
The important lines are:
ssh root#myserver 'python /root/python/run_dev_server.py > /dev/null 2>&1 &'
SERV_PID=$(ssh root#myserver 'echo $!')
the problem with this is that the script finishes but the file ends up empty as well as the $SERV_PID variable.
And if i dont redirect the outputs and just do something like this:
SERV_PID=$(ssh root#myserver 'python /root/python/run_dev_server.py & echo $!')
i get stuck after "Restarting the server" and never get the PID or the file that will contain it or even the end of the script.
But if i run this right in the console:
ssh root#myserver 'python /root/python/run_dev_server.py & echo $!'
i get a PID printed to the terminal.
Any advice on this would be really appreciated.
ssh root#myserver 'python /root/python/run_dev_server.py > /dev/null 2>&1 &'
SERV_PID=$(ssh root#myserver 'echo $!')
With the above code, you are running two ssh commands and the both create two different shells. The problem is echo $! gives the most recent background process' ID from the current shell which is none.
That is, when you ssh for the second time, it's new shell and there's no background process running in it and hence echo $! gives no output. This explains why your PID file is empty.
Instead what you can do is to lookup for all instances of your python script and kill them using killall command. Or similar idea using ps command.
Thanks to Kingslndian i solved it by making one single command that did the three steps i required, so with that avoided the problem of running in different shells:
ssh root#myserver 'python /root/python/run_dev_server.py > /dev/null 2>&1 & echo $! > "dmr.pid"'

Resources