首页 > 代码库 > linux下如何让控制台程序后台运行

linux下如何让控制台程序后台运行

方法1:./test & 
方法2:(./test &) 
方法3:nohup 和bg 

方法4:screen -mS test /root/test   退出程序需要手动进入screen创建的test虚拟终端去退出

方法5:修改并保存一下代码为脚本,运行
#!/bin/bash

NAME=$(basename "$0")
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
DAEMON=/root #程序路径
DESC=alserver#程序描述
USER=root#运行程序的用户

test -x "$DAEMON" || exit 0

case "$1" in
start)
echo -n "Starting $DESC: "
su "$USER" -c "$(printf "%q -q &" "$DAEMON" )"
echo "$NAME."
;;
stop)
echo -n "Stopping $DESC: "
killall --quiet "$DAEMON"
echo "$NAME."
;;
restart|force-reload)
echo -n "Restarting $DESC: "
killall --quiet "$DAEMON"
sleep 1
su "$USER" -c "$(printf "%q -q &" "$DAEMON" )"
;;
*)
printf "Usage: %q {start|stop|restart|force-reload}\n" "$0" >&2
exit 1
;;
esac

exit 0