How to kill certain process running more than 36 hours and containing certain phrasse in its command? [closed] - linux

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 6 years ago.
Improve this question
on the Linux (CentOS 6), i want to kill process containing "pkgacc" in its command (so no exact command but just partial match) if it is running more than 36 hours.
There is related question: How do you kill all Linux processes that are older than a certain age? but any of the solutions provided do not work for me.
when executing:
if [[ "$(uname)" = "Linux" ]];then killall --older-than 1h someprocessname;fi
It just return killall usage page on how to use killall, in its manual page there is no mention about "--older-than" switch.

It is infinitely easier to invoke a program in a wrapper like timeout from GNU coreutils than to go hunting for them after the fact. In particular because timeout owns its process, there is no ambiguity that it kills the right process. Thus
timeout 36h pkgaccess --pkg_option --another_option package_name
where I made up the names and options for the pkgaccess command since you didn't give them. This process will run no longer than 36 hours.

I think you could do something like
ps -eo pid,cmd,etime
then you could parse the output with grep searching for you process,
something like that:
ps -eo pid,cmd,etime | grep pkgacc
you will have some output with one or more result, the last column from the output must be the time of running process, so one more little bash effort
and you could check if the time is greater than 36 hours.
#!/bin/bash
FOO=$(ps -eo pid,cmd,etime | grep -m 1 pkgacc | awk '{ print $1" "$3 }'| sed -e 's/\://g')
IFS=' ' read -r -a array <<< "$FOO"
if [ "${array[1]}" -gt "360000" ]; then
echo "kill the process: ${array[0]}"
else
echo "process was not found or time less than 36 hours"
fi
I think that could solve part of your problem,
see that I do not explicit kill the process but just indicate
what it is. You could improve the idea from that.

Related

How to set the output of ^Z (Control-Z) to output the PID of the stopped process? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 8 months ago.
Improve this question
I start my script bash test.sh and then press Control-Z and get this:
^Z
[1]+ Stopped bash test.sh
All fine. But I want that the output also has the PID.
I know I can do this ps $(jobs -p) afterwards to get the PID. But how it is possible that the output of Control-Z prints directly the PID?
I don't think that's possible. That said, perhaps you can take a step back and clarify why you are hoping to do that?
Because what you can do is directly refer to the particular job with %1 (or %<n> more generally, if you have multiple background jobs) for several built-in commands (fg, bg, kill, ...):
$ sleep 30
^Z
[1]+ Stopped sleep 30
$ kill %1
[1]+ Terminated: 15 sleep 30
More details in man bash or here: https://www.gnu.org/software/bash/manual/bash.html#Job-Control-Basics

Is there a programm in Linux which will execute a command and send an email if it fails? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 7 years ago.
Improve this question
I have a process which is started by Сron like this:
timeout 1h /app/longprocess.sh
Now I want to be notified by email if something goes wrong with it. Imagine this:
notifyme maintainer#example.org timeout 1h /app/longprocess.sh
where notifyme is a supposed command which will send an email to maintainer#example.org with the output of the command in case the command exits with a non-zero status. Is there something like this?
No, but (untested)
notifyme() {
local recipient=$1
shift
output=$( "$#" 2>&1 )
rc=$?
echo "$output"
if [[ $rc -ne 0 ]]; then
mailx -s "Non-zero exit status ($rc) for command: $*" "$recipient" <<< "$output"
fi
}
Use the logical OR operator ||. When || is added to a command, whatever is listed after it will only get executed if the command listed before it returns a non-zero status (that is, if it errors out.) For example, using the code you provided:
timeout 1h /app/longprocess.sh || mail -s "job failed" maintainer#example.org
In the above command, mail -s "job failed" maintainer#example.org will only execute if timeout 1h /app/longprocess.sh is not successful.
Instead of using the || operator as suggested in another answer, I recommend using && and always pinging an external monitoring service. The monitoring service would alert you if a ping is missing. This has the benefit of also getting alerted if your cronjob didn't start up for whatever reason. One such monitor is wdt.io.
Your cronjob would look something like
timeout 1h /app/longprocess.sh && curl somemonitor.com/longprocess

How to kill a particular process in Linux? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 8 years ago.
Improve this question
I would like to kill a particular process (it is a program which name is test.exe) in Linux. To do that I can type:
ps aux | grep test.exe
And then:
kill -9 process_id_of_test_exe
As You can see I need to type commands several times (not immediately). The question is how to do that in one command. Take the process_id of test.exe and give to kill. I think that I need to write a script in such case. Thanks.
Use this command to get PID
pidof test.exe
1044
Then to kill it:
kill 1044
And then join them:
kill $(pidof test.exe)
You can also use your command:
kill -9 $(ps aux | grep [t]est.exe)
The [t] prevents grep from grepping itself.
Try:
killall test.exe
See man killall for more details!
You can combine kill -9 with pgrep like follows:
kill -9 `pgrep test.exe`
For killing all processess with keyword test.exe, try
pkill -9 -f test.exe

How to kill background task from another session? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 9 years ago.
Improve this question
I've run a multithreading program in background:
./my_task &
Then I logged out, then logged again. Now jobs command does not show this program, but top does show the threads of this program. So it is still running. How to stop it? I guess I can kill each thread, but there are many of them and I don't know how it will affect my_task program.
I am using Debian Squeeze.
In common case, you can use
ps aux | grep my_task
-or, if you know, that process name starts with "my_task" exactly:
ps aux | grep [m]y_task
(this will exclude grep process itself from result table)
to get desired process id (let it be $pid) and then kill it with kill $pid
edit (thanks to comments below): jobs is part of bash itself, and so information about it is listed in man bash page:
Job control refers to the ability to selectively stop (suspend) the
execution of processes and continue
(resume) their execution at a later point. A user typically employs this facility via an interactive
interface supplied jointly by the operating system kernel's terminal driver and bash.
The shell associates a job with each pipeline. It keeps a table of currently executing jobs, which may
be listed with the jobs command. When bash starts a job asynchronously (in the background), it prints a
line that looks like:
[1] 25647
indicating that this job is job number 1 and that the process ID of the last process in the pipeline
associated with this job is 25647. All of the processes in a single pipeline are members of the same
job. Bash uses the job abstraction as the basis for job control.
but this will not help a case since it will list jobs only for current instance (which, of cause, will change when you're changing your session)
run your proces with log. I have used gnome-calculator for example:
gnome-calculator & echo $! > tmp/11/mylog
and add below to .bashrc or other autostart for kill it:
kill `cat tmp/11/mylog`
You can use pgrep to find the command:
$ pgrep my_task
4384
Then you can use that output to make sure it's the command you want:
$ ps -fp 4384 | cat
I pipe the output to cat because the ps command will chop off the output at the rowsize of the terminal unless it's piped to another command.
You could combine them too:
$ ps -fp $(pgrep my_task) | cat
You can also use pkill if you're brave:
$ pkill my_task
This will kill any processes that match the regular expression my_task that is owned by the user.

Check a files Modified date and email if it has changed [closed]

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 10 years ago.
Improve this question
I am look for a bash script that will check if a file has been modified in the last hour and email an alert if it has been modified. This script will be used in Solaris and Ubuntu. I am sure it's not hard, but I am not a Linux admin. Can someone please help?
How about this?
#!/bin/bash
[[ -z `find /home/spatel/ -mmin -60` ]]
if [ $? -eq 0 ]
then
echo -e "nothing has changed"
else
mail -s "file has been changed" spatel#example.com
fi
Put this script in hourly cron job
01 * * * * /path/to/myscript
linux supports the inotify command. You use it to monitor file activity - file change, file creation whatever you want, whenever you want.
The find command given above will not work on out-of-the-box solaris. It is fine for linux. You have two options on Solaris:
go to www.sunfreeware.com and download gnu coreutils, which will install gnu find (the above version of find) in /usr/local/bin
write a script that uses the touch command, wait more than 60 minutes then test the file, the only problem is this script runs in the background forever, you can avoid this if you know some perl to generate a timestring suitable for touch -t [time string] to create the file with a time one hour in the past
This is the run forever version:
while true
do
touch dummy
sleep 3615 # 1 hour 15 seconds
[ file_i_want_to_test -nt dummy ] && echo 'file blah changed' |
mailx -s 'file changed' me#myco.com
done

Resources