I'm running a php yii2 cron from cronjob through a sh file every minute, so if previous job doesn't complete within a minute so it won't execute more than 1 time. But my problem is that, it got stuck some time and i have to manually kill this process. How can i kill a cronjob if it running from past 30 minutes.
Currently my sh file is like below:
if ps -ef | grep -v grep | grep cron/my-cron-action; then
exit 0
else
php /home/public_html/full-path-to-project/yii cron/my-cron-action
exit 0
fi
I write a test . Use check_test_demo.sh to detect test_demo.sh
#test_demo.sh
#!/usr/bin/env bash
echo $(date +%s) > ~/temp_timestamp.log
echo process start
while [ 1 ]; do
date
sleep 1
done
#check_test_demo.sh
#!/usr/bin/env bash
process_num=$(ps -ef | grep -v grep | grep test_demo.sh | wc -l)
if [ $process_num -gt 0 ]; then
now_time=$(date +%s)
start_time=$(cat ~/temp_timestamp.log)
run_time=$((now_time - $start_time))
if [ $run_time -gt 18 ]; then
ps aux | grep -v grep | grep test_demo.sh | awk '{print "kill -9 " $2}' | sh
nohup ~/dev-project/wwwroot/remain/file_temp/test_demo.sh > /dev/null 2>&1 &
echo "restart success"
exit
fi
echo running time $run_time seconds
else
nohup ~/dev-project/wwwroot/remain/file_temp/test_demo.sh > /dev/null 2>&1 &
echo "start success "
fi
answer, I don't know if it's right. You can try.
I think you should record the start time of a process first
#!/usr/bin/env bash
process_num=$(ps -ef | grep -v grep | grep cron/my-cron-action | wc -l)
if [ $process_num -gt 0 ]; then
now_time=$(date +%s)
start_time=$(cat ~/temp_timestamp.log)
run_time=$((now_time - $start_time))
if [ $run_time -gt 1800 ]; then
ps aux | grep -v grep | grep cron/my-cron-action | awk '{print "kill -9 " $2}' | sh
echo $(date +%s) > ~/temp_timestamp.log
php /home/public_html/full-path-to-project/yii cron/my-cron-action
echo "restart success"
exit
fi
echo "process running time : $run_time seconds ."
else
echo $(date +%s) > ~/temp_timestamp.log
php /home/public_html/full-path-to-project/yii cron/my-cron-action
echo "process start success"
fi
Related
I never use bash file in laravel before.
I found a github project and I downloaded and try to run it.
I try to run this file in background.
#!/usr/bin/env bash
if [ $1 = "daemon" ]; then
echo "daemon"
while sleep 1
do
PID=$(ps aux | grep 'daemon:ticker' | grep -v grep | awk '{print $2}')
if [[ -z $PID ]]; then
php artisan daemon:ticker &>/dev/null &
fi
PID=$(ps aux | grep 'daemon:signals' | grep -v grep | awk '{print $2}')
if [[ -z $PID ]]; then
php artisan daemon:signals &>/dev/null &
fi
PID=$(ps aux | grep 'daemon:orders' | grep -v grep | awk '{print $2}')
if [[ -z $PID ]]; then
php artisan daemon:orders &>/dev/null &
fi
# PID=$(ps aux | grep 'ssh -D 1337' | grep -v grep | awk '{print $2}')
# if [[ -z $PID ]]; then
# ssh -D 1337 -f -C -q -N root#149.28.135.20
# fi
done
fi
if [ $1 = "status" ]; then
echo "status ..."
PID=$(ps aux | grep 'daemon:ticker' | grep -v grep | awk '{print $2}')
if [[ -z $PID ]]; then
echo "ticker Daemon Stopped"
else
echo "ticker Daemon Running"
fi
PID=$(ps aux | grep 'daemon:signals' | grep -v grep | awk '{print $2}')
if [[ -z $PID ]]; then
echo "Signals Daemon Stopped"
else
echo "Signals Daemon Running"
fi
PID=$(ps aux | grep 'daemon:orders' | grep -v grep | awk '{print $2}')
if [[ -z $PID ]]; then
echo "Orders Daemon Stopped"
else
echo "Orders Daemon Running"
fi
PID=$(ps aux | grep 'ssh -D 1337' | grep -v grep | awk '{print $2}')
if [[ -z $PID ]]; then
echo "Tunnel Daemon Stopped"
else
echo "Tunnel Daemon Running"
fi
fi
if [ $1 = "restart" ]; then
pkill -f "php artisan daemon:"
echo "restarted"
fi
if [ $1 = "stop" ]; then
pkill -f "php artisan daemon:"
pkill -f "services.sh"
echo "stopped"
fi
by this command
sh services.sh
error is
services.sh: line 2: $'\r': command not found services.sh: line 74: syntax error: unexpected end of file
I run this bash file in window.
how can solve this error.
can someone explain me how use .sh file in laravel project.
I want to understand basic concept also.
you have $1 in your script but when you run script you do not have any input to your script
What i want to achieve
I want to execute some script it it's process in not started on the server. so for that i am preparing the command in shell script and executing it in single line.
Command with php variable
$cmd = "if [[ `ps auxww | grep -v grep | grep ".$process_file." | grep '".$find."'` == '' ]] ; then ".$cmd2." fi";
echo $cmd."\n";
Executed command, once variables are replaced (what will actually run on bash):
if [[ `ps auxww | grep -v grep | grep /home/new_jig.php | grep 'test_51 1714052'` == '' ]] ; then php /home/new_jig.php test_51 1714052 & fi;
executing command
exec($cmd,$out,$res);
Please note that, I have also split the problem in to two statement and execute those. But it is time consuming. It is causing problems when I have more than 2000 in list, and the command is executed for all. This takes about 1 or more than 1 minute to reach to the last number.
I want to achieve this within 10 seconds. Please help me to reach optimum output.
Thanks
Jignesh
somehow I am able to make it execute with the following command
$process_file = phpfile which executing some functionality
$cmd2 = " php ".$process_file." 1212 >/dev/null 2>/dev/null & ";
$cmd11 ="if ps -auxw | grep -v grep | grep '".$process_file."' | grep '".$find."' &> /dev/null ; then echo 1;".$cmd2."; fi";
shell_exec($cmd11." >/dev/null 2>/dev/null &");
Before this: for 1100 request the process was taking about 60+ seconds
After this: it is getting completed between 20 to 30 seconds
I have a script to get server health from multiple servers like this:
#!/bin/bash
for ip
do
ssh 192.168.1.209 ssh root#$ip cat /proc/loadavg | awk '{print $1}' #CPU Usage
free | grep Mem | awk '{print $3/$2 * 100.0}' #Memory Usage
df -khP | awk '{print $3 "/" $2}' | awk 'FNR == 2' #Disk Space
df -kihP | awk '{print $3 "/" $2}' | awk 'FNR == 2' #Inode Space
date +'%d %b %Y %r %Z' #Datetime
ps -eo user,pid,pcpu,pmem,args|sort -nr -k3|head -5 #Process
done
The 209 is acting like a portal on my network so I have to ssh to it 1st in order to access other servers. By typing this command on terminal:
./my_script.sh 192.168.1.210 192.168.1.211 192.168.1.212
I would like to get each of the command output (ps,date etc) from each server. Expected output for 2 servers should be like:
0.11 #health from server 1
4.82577
1.7G/49G
46K/49M
27 Dec 2016 05:34:57 PM HKT
root 93 0.0 0.0 [kauditd]
root 9 0.0 0.0 [rcuob/0]
root 8740 0.0 0.0 ifstat --scan=100
root 829 0.0 0.0 /usr/sbin/wpa_supplicant -u -f /var/log/wpa_supplicant.log -c /etc/wpa_supplicant/wpa_supplicant.conf -u -f /var/log/wpa_supplicant.log -P /var/run/wpa_supplicant.pid
0.00 #health from server 2
4.82422
1.7G/49G
46K/49M
27 Dec 2016 05:34:57 PM HKT
root 93 0.0 0.0 [kauditd]
root 9 0.0 0.0 [rcuob/0]
root 8740 0.0 0.0 ifstat --scan=100
root 829 0.0 0.0 /usr/sbin/wpa_supplicant -u -f /var/log/wpa_supplicant.log -c /etc/wpa_supplicant/wpa_supplicant.conf -u -f /var/log/wpa_supplicant.log -P /var/run/wpa_supplicant.pid
The problem that I'm facing is that It seems like it's only getting the health info from one server only. Why is that? Is it because I cannot do SSH like this? I'm using PHP exec() function to execute the script btw, to further format and display it on my local page.
The best way to do that in bash is to use Here Documents << and run a loop over each of the arguments ($#) passed to the script as
for ip in "$#"
do
ssh 192.168.1.209 ssh root#"$ip" <<-EOF
cat /proc/loadavg | awk '{print $1}'
free | grep Mem | awk '{print $3/$2 * 100.0}'
df -khP | awk '{print $3 "/" $2}' | awk 'FNR == 2'
df -kihP | awk '{print $3 "/" $2}' | awk 'FNR == 2'
date +'%d %b %Y %r %Z'
ps -eo user,pid,pcpu,pmem,args|sort -nr -k3|head -5
EOF
done
Remember to NOT have leading or trailing whitespaces before header <<-EOF and the final EOF, use tab-space for the terminating EOF.
You can run the script now as
./my_script.sh 192.168.1.210 192.168.1.211 192.168.1.212
Also you can wrap the contents of the script in a simple bash script and run it in one shot as
#!/bin/bash
cat /proc/loadavg | awk '{print $1}'
free | grep Mem | awk '{print $3/$2 * 100.0}'
df -khP | awk '{print $3 "/" $2}' | awk 'FNR == 2'
df -kihP | awk '{print $3 "/" $2}' | awk 'FNR == 2'
date +'%d %b %Y %r %Z'
ps -eo user,pid,pcpu,pmem,args|sort -nr -k3|head -5
Call the above script as commandlist.sh and call it inside the for-loop as
ssh 192.168.1.209 ssh root#"$ip" 'bash -s ' < /path-to/commandlist.sh
Well, I’d do something completely different.
First I’d write a commandlist.sh script as follows (not forgetting to make it executable...):
#!/bin/bash
echo "# Health from $(hostname)"
cat /proc/loadavg | cut -d' ' -f1 #CPU Usage
free | grep Mem | awk '{print $3/$2 * 100.0}' #Memory Usage
df -khP | awk 'FNR == 2 {print $3 "/" $2}' #Disk Space
df -kihP | awk 'FNR == 2 {print $3 "/" $2}' #Inode Space
date +'%d %b %Y %r %Z' #Datetime
ps -eo user,pid,pcpu,pmem,args|sort -nr -k3|head -5 #Process
(mmh.. I would produce the same output with very different code, but since it’s your script I’ve only edited out a couple of superfluous awk invocations.)
Then, I’d place it on the 209, where I’d also install GNU parallel.
Separately, I’d write the file ~/.parallel/sshloginfile as follows:
1/192.168.1.210
1/192.168.1.211
1/192.168.1.212
and I’d run the command
ssh 192.168.0.209 parallel -S .. --nonall --bf commandlist.sh ./commandlist.sh
Have a good look at man parallel for more possibilities.
I have a button in my program that when pressed, runs this line of code.
ps -eo pid,command | grep "test-usb20X" | grep -v grep | awk '{print $1}'
It gives me the correct pid output. For example, "3243".
What I want to do now is kill that pid. I would just write
exec("kill -9 3232");
But the pid changes, so how do I save that number to a variable and then use it in the kill line?
You may write the response of your first command to a variable
$pid = exec("ps -eo pid,command | grep \"test-usb20X\" | grep -v grep | awk '{print $1}'");
and use this variable for the next command
exec("kill -9 ".$pid);
I have setup cron job, it is working properly,when i directly runs the php file(from the path/url) it successfully runs, but from cron its not. i have used simple php mail function and its is running successfully but my task is not completed properly. Kindly suggest me the possible areas to look into it. (I am using Cpanel). here this is the code that i am using in cron.ch
#!/bin/sh
# location of the php binary
if [ ! "$1" = "" ] ; then
CRONSCRIPT=$1
else
CRONSCRIPT=cron.php
fi
PHP_BIN=`which php`
# absolute path to magento installation
INSTALLDIR=`echo $0 | sed 's/cron\.sh//g'`
# prepend the intallation path if not given an absolute path
if [ "$INSTALLDIR" != "" -a "`expr index $CRONSCRIPT /`" != "1" ];then
if ! ps auxwww | grep "$INSTALLDIR""$CRONSCRIPT" | grep -v grep 1>/dev/null 2>/dev/null ; then
$PHP_BIN "$INSTALLDIR""$CRONSCRIPT" &
fi
else
if ! ps auxwww | grep " $CRONSCRIPT" | grep -v grep | grep -v cron.sh 1>/dev/null 2>/dev/null ; then
$PHP_BIN $CRONSCRIPT &
fi
fi
this is how i am setting cron for every 15 minutes from c-panel
*/15 * * * *