Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/jpa/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Linux 用于终止早于x时间的进程的Bash脚本-表达式中出现语法错误_Linux_Bash_Process_Centos_Centos7 - Fatal编程技术网

Linux 用于终止早于x时间的进程的Bash脚本-表达式中出现语法错误

Linux 用于终止早于x时间的进程的Bash脚本-表达式中出现语法错误,linux,bash,process,centos,centos7,Linux,Bash,Process,Centos,Centos7,我正在尝试编写一个脚本,以在进程超过5分钟时按ID终止该进程。我已经知道$pid中的进程ID pid=1234 # 300 seconds = 5min maximum_runtime=300 process_start_time=`ps -o lstart= -p $pid` current_time=`date +%s` let diff=${current_time}-${process_start_time} if [ $diff -gt $maximum_runtime ] t

我正在尝试编写一个脚本,以在进程超过5分钟时按ID终止该进程。我已经知道
$pid
中的进程ID

pid=1234

# 300 seconds = 5min
maximum_runtime=300

process_start_time=`ps -o lstart= -p $pid`
current_time=`date +%s`

let diff=${current_time}-${process_start_time}

if [ $diff -gt $maximum_runtime ]
then
    kill -3 $pid
fi
这将导致一个错误:

./script.sh:第9行:let:11:41:01:表达式中的语法错误(错误标记为“:41:01”)


我使用了来自的一些代码。有什么办法解决这个问题吗?

您正在尝试减去像
1563208699
2019年7月12日星期五12:26:51
这样的值,当然这是一个错误
pid=1234

# 300 seconds = 5min
maximum_runtime=300

process_start_time=`ps -o lstart= -p $pid`
current_time=`date` #got the tiem in same format

let diff="$(($(date -d "$current_time" '+%s') - $(date -d "$process_start_time" '+%s')))"
#result for diff in secconds, calculated using date tool

if [ $diff -gt $maximum_runtime ]
then
    kill -3 $pid
fi