modify scripts
This commit is contained in:
58
bin/service.sh
Executable file
58
bin/service.sh
Executable file
@ -0,0 +1,58 @@
|
||||
#!/bin/bash
|
||||
|
||||
# 确保脚本使用一个参数
|
||||
if [ $# -ne 1 ]; then
|
||||
echo "Usage: $0 {start|stop|restart}"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# 获取当前脚本所在目录
|
||||
script_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
||||
program_name="aigrammar"
|
||||
program_path="$script_dir/$program_name"
|
||||
|
||||
# 配置和日志目录
|
||||
config_file="$script_dir/../conf/config.toml"
|
||||
log_dir="$script_dir/../log"
|
||||
log_file="$log_dir/output.log"
|
||||
pid_file="$script_dir/${program_name}.pid"
|
||||
|
||||
# 确保日志目录存在
|
||||
mkdir -p "$log_dir"
|
||||
|
||||
case "$1" in
|
||||
start)
|
||||
echo "Starting $program_name..."
|
||||
if [ -f "$pid_file" ] && kill -0 $(cat "$pid_file") 2>/dev/null; then
|
||||
echo "$program_name is already running."
|
||||
exit 1
|
||||
fi
|
||||
nohup "$program_path" --config="$config_file" > "$log_file" 2>&1 &
|
||||
echo $! > "$pid_file"
|
||||
echo "$program_name started with PID $(cat "$pid_file")."
|
||||
;;
|
||||
|
||||
stop)
|
||||
echo "Stopping $program_name..."
|
||||
if [ -f "$pid_file" ]; then
|
||||
kill -TERM $(cat "$pid_file") 2>/dev/null && rm -f "$pid_file"
|
||||
echo "$program_name stopped."
|
||||
else
|
||||
echo "No PID file found, attempting pkill..."
|
||||
pkill -f "$program_name" && echo "$program_name stopped."
|
||||
fi
|
||||
;;
|
||||
|
||||
restart)
|
||||
echo "Restarting $program_name..."
|
||||
$0 stop
|
||||
sleep 2
|
||||
$0 start
|
||||
;;
|
||||
|
||||
*)
|
||||
echo "Unknown command: $1"
|
||||
echo "Usage: $0 {start|stop|restart}"
|
||||
exit 1
|
||||
;;
|
||||
esac
|
||||
Reference in New Issue
Block a user