Of course you can, here's a simple example:
- First, you need to create a startup script, say called
myservice.sh
, and place it/etc/init.d/
in a directory. This script needs to contain the following:
#!/bin/bash
# chkconfig: 345 99 10
# description: My service
case "$1" in
start)
echo "Starting my service"
# 这里写启动服务的命令
;;
stop)
echo "Stopping my service"
# 这里写停止服务的命令
;;
restart)
$0 stop
$0 start
;;
*)
echo "Usage: $0 {start|stop|restart}"
exit 1
;;
esac
exit 0
- Next, you need to add this script to the system service, you can use the following command:
sudo chkconfig --add myservice.sh
- Then, you need to set the startup level of the service, you can use the following command:
sudo chkconfig myservice.sh on
In this way, your service will automatically start when the system starts. If you need to manually start or stop the service, you can use the following commands:
sudo service myservice.sh start
sudo service myservice.sh stop
sudo service myservice.sh restart
Hope this example can help you.