0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

闇のプログラミングAdvent Calendar 2023

Day 4

n分以上起動しているプロセスをkillするスクリプト

Last updated at Posted at 2023-12-03

スクリプト

  • クソマルコフ連鎖アルゴリズム組んだ時に暴走したスクリプトを止めるために作ったクソスクリプト
  • cronで1分に1回動かせば"$ElapsedTime" -gt 300で設定した以上の時間動いているプロセスを殺す
#!/bin/bash
if [ "$#" -ne 1 ]; then
  echo "Argment Error"
  exit 1
fi

ProcessName="$1"
PID=$$

for i in $(ps -ef | grep "$ProcessName" | grep -v "$PID" | grep -v grep | awk '{print $2}')
do
  TIME=$(ps -o lstart --noheader -p "$i")
  if [ -n "$TIME" ]; then
    StartupTime=$(date +%s -d "$TIME")
    CurrentTime=$(date +%s)
    ElapsedTime=$((CurrentTime - StartupTime))
  else
    ElapsedTime=1
  fi

  if [ "$ElapsedTime" -gt 300 ]; then
    echo "-----------------------------pkill info"
    echo "$(date)"
    ps ef -p "$i"
    kill "$i"
  fi
done
  • 使い方
./kill_task.sh "/ubuntu/kuso_proguramu"

/ubuntu/kuso_proguramu下で動いているやついたら殺す

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?