描述
- 检测应用进程,判断有无已存在进程
- 没有,直接启动
- 有则需要用户二次确认,确认后完成重新启动脚本
脚本
#!/bin/bash
user_path=/home/youruser/opt
app_name='navicat16-premium'
app_path=${user_path}/navicat16-premium-cs.AppImage
log_file=${user_path}/log_${app_name}.log
#后台运行程序
run_app() {
echo "启动应用"
nohup ${app_path} > ${log_file} 2>&1 &
}
kill_if_exists() {
running_process_num=`ps -ef|grep ${app_name}|grep -v grep|wc -l`
if [ $running_process_num -gt 0 ];then
read -p "程序正在运行,是否关闭现有进程?" word
if [ -z ${word} ];then
echo "清确认!"
elif [ "${word}" = "Y" ];then
ps -ef|grep ${app_name}|grep -v grep|awk '{print $2}'|xargs kill -9
echo "清理成功"
return 1;
else
echo "已取消"
fi
return 0;
else
echo '不需要清除'
return 1;
fi
}
start_up() {
kill_if_exists
if [ $? -eq 1 ];then
run_app
else
echo "取消启动"
fi
}
start_up
|