Strip process pid to log_file_name (weird behaviour when running from cron) - bash

I am curious if anyone have similar issue when running scripts from cron. This line of script should copy opentsdb_daemon.log file to opentsdb_daemon_with_pid.log. Currently openTSDB is running on only one PID.
!/bin/sh
cp -f /opt/opentsdb/opentsdb-2.2.0/var/log/opentsdb/opentsdb_daemon.log "/opt/opentsdb/opentsdb-2.2.0/var/log/opentsdb/opentsdb_daemon_pid_$(ps -ef | grep [o]pentsdb | awk '{print $2}').log
It runs ok. File opentsdb_daemon_pid_76079.log is created but when running fron cron it's creating something like this: opentsdb_daemon_pid_63453?63454?76079.log
I have tried to run it from diffrent cron users - with same effect. I would be extremely grateful for any advice.

The command ps -ef | grep [o]pentsdb | awk '{print $2}' should return multiple PID at the time cron run it, what you get is all the PID separated by "?"
The "?" is because \n is not correctly diplayed in the filename
I assume that it is because when cron executes the command, the command appears in the process list, so :
grep [o]pentsdb is also grep by grep [o]pentsdb ;)
You can determine it by the two consecutive PID 63453 and 63454 which are the process lines "cron execute the command xxx" and the child of this process which is the "command xxx"
Maybe a solution could be to add something like :
$(ps -ef | grep [o]pentsdb |grep -Ev "grep|cron" | awk '{print $2}')

Related

Bash shell script command executes out of order when run through Bitrise

In the bitrise workflow's script step, I added following snippet:
adb shell ps | grep screenrecord | awk ‘{print $2}’ | xargs adb shell kill
Purpose is to kill the process called screenrecord that was started in a previous step and it works fine when I test it on my system. But when this workflow is triggered through bitrise, it fails with following logs:
What is the cause of this issue and how to fix it?
Most likely this is because awk is not outputting the process id. One possible workaround to try is the following:
adb shell ps | grep screenrecord | sed -E 's/[ ]+/ /g' | cut -d' ' -f2 | xargs adb shell kill
where the awk command has been substituted with sed (to remove the multiple spaces) and a cut one (to get the process id).

How do I make a stop after a run?

If I do a:
npm run script
Can I stop it with a stop?
npm stop script
Why I tried it and it does not work.
I know that with the combination of "Ctrl + c" I kill it, but I want to do it by command.
Try something like that:
ps -ef | grep script | awk '{print $2}' | head -n1 | xargs kill -9
This command should find first process named script on the list of all unix processes created by all users and kill it by with using its PID.

Kill command won't work correctly in bash script

I was running an ubuntu console, when I type the following command, all the processes would be perfectly killed.
kill -9 $(ps -ef | grep 'job1/' | grep -v grep| awk '{print $2}')
But when I was trying to use crontab to call a script routinely, things went wrong.
#!/bin/bash
pid=$(ps -ef | grep 'job1/' | grep -v grep | awk '{print $2}')
echo $pid
kill -9 $pid
# the following commands were never executed
sleep 5
/data/job1/tomcat8/bin/startup.sh
The result was just like this:
15432 15438
Killed
It seems to just killed the job, but won't execute the following commands. Any idea?
If you are going to make a script that kills things by PID then you need to be very careful that you kill the right things.
You already have grep -v grep to avoid killing the grep itself, but it seems that you have not put in anything to protect against the script killing itself. Since you know your own PID you could grep -v that, but what if you are 123 and one of the things you want to kill is 1234? Probably safer to go by script name.

Grep output of command and use it in "if" statement, bash

Okay so here's another one about the StarMade server.
Previously I had this script for detecting a crash, it would simply search through the logs:
#!/bin/bash
cd "$(dirname "$0")"
if ( grep "[SERVER] SERVER SHUTDOWN" log.txt.0); then
sleep 7; kill -9 $(ps -aef | grep -v grep | grep 'StarMade.jar' | awk '{print $2}')
fi
It would find "[SERVER] SERVER SHUTDOWN" and kill the process after that, however this is not a waterproof method, because with different errors it could be possible that the message doesn't appear, rendering this script useless.
So I have this tool that can send commands to the server, but returns an EOF exception when the server is in a crashed state. I basically want to grab the output of this command, and use it in the if-statement above, instead of the current grep command, in such a way that it would execute the commands below when the grep finds "java.io.EOFException".
I could make it write the output to a file and then grep it from there, but I wonder, isn't there a better/more efficient method to do this?
EDIT: okay, so after a bit of searching I put together the following:
if ( java -jar /home/starmade/StarMade/StarNet.jar xxxxx xxxxx /chat) 2>&1 > /dev/null |grep java.io.EOFException);
Would this be a valid if-statement? I need it to match "java.io.EOFException" in the output of the first command, and if it matches, to execute something with "then" (got that part working).
Not sure to solve your problem, but this line:
ps -aef | grep -v grep | grep 'StarMade.jar' | awk '{print $2}'
could be change to
ps -aef | awk '/[S]tarMade.jar/ {print $2}'
The [S] prevents awk from finding itself.
Or just like this to get the pid
pidof StarMade.jar

Redirecting the output of ps command,getting process id and killing that process using shell script

I want to write a shell script to find the running process for a given user and kill the process by getting the respective process ID.
Its like
ps -ef | grep dinesh
After this, i am getting the output as the following
dinesh 19985 19890 0 11:35 pts/552 00:00:00 grep dinesh
Here 19985 is the process ID. I want to kill that process.
How can i achieve this using script?
I have to parse the ps command output and get the process ID
Thanks in advance.
kill `ps -ef | grep dinesh | awk '{ print $2 }'`
What if there is more than one process defined by the string 'dinesh'? What about the grep process itself? This is a more complete answer
ps -ef | grep dinesh | grep -v grep | awk '{print $2}' | xargs kill -9

Resources