Command output, pipe, script co-operation - linux

I'm writing a small script to restart my lighttpd server:
1. kill already running process
2. start new server
The script is the following:
PID=$(ps aux | grep lighttpd | grep -v grep | cut -c9-15)
kill $PID
sudo lighttpd -f /etc/lighttpd/lighttpd.conf
My problem is that in a terminal window the command
ps aux | grep lighttpd | grep -v grep | cut -c9-15
gives the result: 11685 but if it runs within the shell script than the result is 11685 13339 13340
What am I missing here?
The ps output of the line containing the lighttpd job is
root 11685 0.0 0.0 11096 1960 ? S 16:40 0:00 lighttpd -f /etc/lighttpd/lighttpd.conf

Why you are wasting time in Linux/unix for grepping PID and killing it when you have killall command
/usr/bin/killall
You can directly
killall lighttpd or /usr/bin/killall lighttpd
if not superuser then use sudo
sudo killall lighttpd or sudo /usr/bin/killall lighttpd
can use preferably -9 with killall like in your case would be sudo killall lighttpd
then restart it via
sudo lighttpd -f /etc/lighttpd/lighttpd.conf
if you are looking for fully automated script then make use of except commands
Click here

Related

How to stop/kill airflow scheduler started in daemon mode

I and new to airflow and accidentally started airflow scheduler in daemon mode. Now, I want to kill the scheduler and possibly restart it. I tried doing
sudo kill -9 <list of pids>
pkill <name>
nothing is happening. When I run
ps aux | grep 'airflow scheduler'
I see these entries:
user1 2907 6.0 1.0 329788 62996 ? Sl 17:37 1:26 /users/user1/anaconda2/bin/python /users/user1/anaconda2/bin/airflow scheduler -D
user1 2909 0.0 0.9 327576 58948 ? Sl 17:37 0:00 /users/user1/anaconda2/bin/python /users/user1/anaconda2/bin/airflow scheduler -D
user1 2910 0.0 0.9 327576 58944 ? Sl 17:37 0:00 /users/user1/anaconda2/bin/python /users/user1/anaconda2/bin/airflow scheduler -D
user1 2911 0.0 0.9 327576 58944 ? Sl 17:37 0:00 /users/user1/anaconda2/bin/python /users/user1/anaconda2/bin/airflow scheduler -D
...and so on for 35 lines with different pids.
Any recommendation as to how I can stop/kill airflow scheduler without restarting my machine. I have also checked the pid file for scheduler and tried killing that pid but no effects.
Any help is appreciated. Thanks!
Unfortuntely
kill $(ps -ef | grep "airflow scheduler" | awk '{print $2}')
I was not able to find a clean solution.
Also looking into the code
https://github.com/apache/incubator-airflow/blob/master/airflow/bin/cli.py
Go to the airflow directory where the pid file is and use:
cat airflow-scheduler.pid | xargs kill
Another alternative is:
/usr/bin/rm -f ${AIRFLOW_HOME}/airflow-scheduler.pid
/usr/bin/pkill -f "airflow-scheduler"
Please note better to remove older daemon .pid file; to avoid any issue while restarting on daemon mode.
You can also see: https://github.com/apache/airflow/issues/77
/bin/sh -c 'PATH=/bin:/sbin:/usr/bin:/usr/sbin mPPID=`cat ${AIRFLOW_HOME}/airflow-scheduler.pid`;ps -o pid= --ppid $mPPID | xargs kill -15 && kill -15 $mPPID && rm -f ${AIRFLOW_HOME}/airflow-scheduler.pid'"
But this command relies on parent PID (.pid file) so if parent process was removed and child processes are still running it won't work.
So in my opinion accepted answer is the best. Or if you have installed use pkill
If using any monitoring service i.e monit like in the github link.
Valid stop command would be:
gPPID=`ps -ef | /usr/bin/grep 'airflow scheduler' | /usr/bin/grep -v grep | awk '{print $2}' `; echo $gPPID; | xargs kill -15 && rm -f ${AIRFLOW_HOME}/airflow-scheduler.pid
cd ~/airflow
cat airflow-scheduler.pid | xargs kill
To kill airflow webserver and scheduler, you can use below command
If you have supervisord configured for airflow, then stop it first
supervisorctl stop all
kill -9 `ps aux | grep airflow | awk '{print $2}'`

grepping the PID of a process - Unix

I'm trying to execute the following command:
ps aux | grep com.scheduler.app.workermain | kill -15 [pid]
How can I obtain the [pid] (or list of PID) using ps aux | grep "expression" and pipe that to kill? There may be zero or many processes running the machine. This is part of an automated job, to ensure all the processes spun will be terminated.
A sample line from the command line, when ps aux | grep com.scheduler.app.workermain is executed is:
jenkins 12373 1.1 4.2 2905440 173628 ? Sl 19:28 0:05 java -Xmx600m -Dlog4j.configurationFile=log4j2-trace.xml -Dpid=foobar -Dipaddr=127.0.0.1 -cp build/classes:build/dependency/* com.scheduler.app.workermain testing.properties
pkill is used for exactly this purpose. How about:
pkill -15 -f com.scheduler.app.workermain
Also if you just want to grep for a PID you can use pgrep:
pgrep -f com.scheduler.app.workermain
pkill man page
kill -15 $(ps aux | grep -i com.scheduler.app.workermain | awk -F' ' '{ print $2 }')
One of possible solutions is to use the pidof command:
kill $( pidof com.scheduler.app.workermain )
PS. You don't need to pass -15 (or -TERM) to the kill command, as SIGTERM is the default signal sent.

kill nohup not working with kill -9 PID

I have server running with nohup on port 80. I try
ps aux | grep nohup
and get
root 9234 0.0 0.1 11740 932 pts/1 S+ 15:19 0:00 grep --color=auto nohup
I then try kill -9 11740 (which I believe is the PID) and get an error stating 'no such process." I can't figure out how else to remove this. Any help is appreciated. Thanks
11740 is virtual memory size. PID is the second field, 9234.
The process in your output is grep command itself, not nohup.
You won't see standalone nohup process. When you start some process with nohup my_executable, nohup closes/redirects stdin/stdout/stderr properly, setups necessary signal handlers and replaces itself with my_executable. Search instead for executable which was started with nohup, e.g. ps aux | grep my_executable | grep -v grep
The process you are seeing is the process from your grep command. So by the time you are trying to kill it, the process is already over.
To keep it out of the output use:
ps aux | grep nohup | grep -v 'grep'
It looks like you don't have a nohup process running

how to stop kibana (not as a service)?

I am trying to stop kibana on SSH with kill but it respawns immediatly
dagatsoin#dagatsoin.io [~/logstash]# ps aux | grep kibana
533 28778 0.0 0.0 9292 876 pts/2 S+ 00:16 0:00 grep kibana
dagatsoin#dagatsoin.io [~/logstash]# kill -kill 28778
-bash: kill: (28778) - Aucun processus de ce type
dagatsoin#dagatsoin.io [~/logstash]# ps aux | grep kibana
533 28780 0.0 0.0 9292 876 pts/2 S+ 00:16 0:00 grep kibana
dagatsoin#dagatsoin.io [~/logstash]#
How do you kill this process ?
As mentioned the output that you are seeing is from the ps aux | grep kibana command that you ran. I'm guessing you started kibana using by running the kibana scipt in the bin directory. In this case do something like this:
ps -ef | grep '.*node/bin/node.*src/cli'
Look for the line that looks something like this:
username 5989 2989 1 11:33 pts/1 00:00:05 ./../node/bin/node ./../src/cli
Then run a kill -9 5989 If you have multiple output rows make sure you kill the correct process.
You try to kill your grep process, not kibana service who is not running currently.
For me, nothing worked on Ubuntu 18, except this:
systemctl stop snap.kibana.kibana
You kibana process is not running. when you do run a ps aux command and pipe it with a grep command, grep will be running as a separate process. hence its shows up in the ps aux output as process no 28778 or 28780. you can observe that the process number is changing. every-time you stop the ps aux grep process also ends as it is piped with ps and it starts as a new process(id) when you rerun it. hence you are getting an error when you run kill -kill 28778 as it has already stopped.
When you start your Kibana using the start script in $KIBANA_HOME/bin/kibana, this script runs another script in $KIBANA_HOME/node/bin/node. Remember, you must search using ps command for a different name (not Kibana name).
So I use ps awwwx | grep -i ./bin/../node/bin/node | grep -v grep on new Linux versions like Ubuntu 18 (or ps -ef | grep -i ./bin/../node/bin/node | grep -v grep on older versions) to search for the Kibana node. This command will give you the accurate PID and you can kill the Kibana node via mentioned PID.
grep -v grep part of this command is just to omit the unnecessary lines from the output of the command.

capture pid of tomcat to kill using bash script in Linux

I am trying to write a bash script that finds the PID of tomcat6, kills it, starts it again, then waits 1 minute, then kills the process again and starts it again.
This is what I have so far but I am struggling to have the script kill tomcat using the previous pid:
ps aux | grep tomcat6
kill -9 $!
service Tomcat6 start
sleep 1m
ps aux | grep tomcat6
kill -9 $!
service Tomcat6 start
Thanks
T_PID=$(ps aux | grep Tomcat6 | awk 'NR==1{print $2}')
kill -9 $T_PID
service Tomcat6 start
sleep 1m
If grep gives multiple outputs, NR==x will only get the PID of desired line.
You can just use
pkill tomcat6
To do it only one time it's enough (if you need sudo).
sudo service tomcat6 restart ; sleep 1m ; sudo service tomcat6 restart ;
or even better
sudo /bin/bash -c "service tomcat6 restart; sleep 1m; service tomcat6 restart;"
This can be a never ending loop that you have to interrupt by hands.
Take it like a scratch.
#!/bin/bash
while :
do
pkill tomcat6 # or pkill -9 tomcat6
service tomcat6 start
sleep 1m
done
If I remember well it exists the possibility to restart that service.
If so you can use even
#!/bin/bash
while :
do
service tomcat6 restart
sleep 1m
done

Resources