I need a shell script to kill a particular running process after a specific time by getting the process name and time as input.
I’m using centos machine i’ve tried the script but couldn’t complete on killing the process on particular timing.
#!/bin/bash
read -p 'Process: ' name
read -p 'Timecontrol: ' time
ps -ef | grep $name | awk '{print $5}'
pkill -9 "$name"
the expected output to be kill the process in specific time which will be given as input.
3
Answers
You can use a cron job to terminate the process in specific date and time.
If you have to use a script:
Note:
#
if you plan to run this script forever.The script will not kill the process at that particular time which you want to give it as an input as the script will run and die it will not wait for a specific time.
You can tickle this in two ways.
awk '{print $2}'
this returnPID
in ubuntu, you have to check-in Centos if it returnsPID
if not then change it toawk '{print $5}'
So you can run with
With Cron job, you do not need to pass time, just pass the name of the process and the script will run on the specified time and will kill the process.
Create-cron-job-on-CentOS
0 0 * * * /path_to_script/kill_process.sh node
This will kill process every-day-at-midnight
[Note: The input for time must be in seconds only, i.e 120 for 2 minutes]
Sample Input: