1.按一定的规则编写服务脚本,比如:myserviced
#!/bin/sh
#chkconfig: 2345 80 05
#description: myservice
case $1 in
start)
echo "myservice startup" #将该行替换成你自己的服务启动命令
;;
stop)
echo "myservice stop" #将该行替换成你自己服务的启动命令
;;
restart)
echo "myservice stop" #...
echo "myservice startup" #...
;;
*)
;;
esac
命令解析:
# chkconfig: 2345 80 5
2345表示服务的运行级别,80代表Start的顺序,05代表Kill(Stop)的顺序;
# description: service_description
该服务的描述
2.将编写的脚本放到/etc/init.d/,将myserviced的访问权限加上“可执行”
[plain] view plain copy print?
chmod +x myserviced
3.增加服务
[plain] view plain copy print?
chkconfig --add myserviced
4.启停服务
[plain] view plain copy print?
service myserviced start
service myserviced stop
服务添加完成
可以用:chkconfig --list查看当前系统的服务
可以用:chkconfig --del myserviced删除服务
5.添加开机自启动
chkconfig --level 2345 myservice off
systemctl is-enabled test.service #查询服务是否开机启动
#systemd查看开机自启动的程序
ls /etc/systemd/system/multi-user.target.wants/
#将进程服务在后台运行
nohup shell命令 &
0、1和2分别表示标准输入、标准输出和标准错误信息输出,可以用来指定需要重定向的标准输入或输出。
由于使用nohup时,会自动将输出写入nohup.out文件中,如果文件很大的话,nohup.out就会不停的增大,这是我们不希望看到的,因此,可以利用/dev/null来解决这个问题。
nohup shell命令 >/dev/null 2>log &
如果错误信息也不想要的话:
nohup shell命令 >/dev/null 2>&1 &
#!/bin/sh
#chkconfig: 2345 80 05
#description: myservice
case $1 in
start)
echo "myservice startup" #将该行替换成你自己的服务启动命令
;;
stop)
echo "myservice stop" #将该行替换成你自己服务的启动命令
;;
restart)
echo "myservice stop" #...
echo "myservice startup" #...
;;
*)
;;
esac
命令解析:
# chkconfig: 2345 80 5
2345表示服务的运行级别,80代表Start的顺序,05代表Kill(Stop)的顺序;
# description: service_description
该服务的描述
2.将编写的脚本放到/etc/init.d/,将myserviced的访问权限加上“可执行”
[plain] view plain copy print?
chmod +x myserviced
3.增加服务
[plain] view plain copy print?
chkconfig --add myserviced
4.启停服务
[plain] view plain copy print?
service myserviced start
service myserviced stop
服务添加完成
可以用:chkconfig --list查看当前系统的服务
可以用:chkconfig --del myserviced删除服务
5.添加开机自启动
chkconfig --level 2345 myservice off
systemctl is-enabled test.service #查询服务是否开机启动
#systemd查看开机自启动的程序
ls /etc/systemd/system/multi-user.target.wants/
#将进程服务在后台运行
nohup shell命令 &
0、1和2分别表示标准输入、标准输出和标准错误信息输出,可以用来指定需要重定向的标准输入或输出。
由于使用nohup时,会自动将输出写入nohup.out文件中,如果文件很大的话,nohup.out就会不停的增大,这是我们不希望看到的,因此,可以利用/dev/null来解决这个问题。
nohup shell命令 >/dev/null 2>log &
如果错误信息也不想要的话:
nohup shell命令 >/dev/null 2>&1 &