service linux 权限,Linux service 创建和注册

操作系统:Centos  Linux 7.6

1、方法一:Centos6

此方法主要是Cenos7 之前的系统服务注册方式,目前依然被Centos7兼容

1、创建服务文件

在/etc/rc.d/init.d目录下创建ruoli_service服务文件

[root@localhost init.d]# cd /etc/rc.d/init.d

[root@localhost init.d]# vi ruoli_service

ruoli_service文件内容如下

start() {

echo "ruoli service start ... "

sleep 2

}

stop() {

echo "ruoli service stop ..."

}

status() {

echo "ruoli service status"

}

case "$1" in

start)

start

;;

stop)

stop

;;

reload|restart)

stop

start

;;

status)

status $SNAME

;;

*)

echo $"Usage: $0 {start|stop|restart|status}"

exit 1

esac

2、授予可执行权限

[root@localhost init.d]# chmod +x ruoli_service

3、运行服务

[root@localhost init.d]# service ruoli_service start

ruoli service start ...

[root@localhost init.d]# service ruoli_service stop

ruoli service stop ...

[root@localhost init.d]# service ruoli_service status

ruoli service status

[root@localhost init.d]# service ruoli_service restart

ruoli service stop ...

ruoli service start ...

[root@localhost init.d]#

2、方法二:Centos7

1、创建服务文件

在目录 /usr/lib/systemd/system/ 下新建 activemq.service 服务注册文件,内容如下:

[Unit]

Description=activemq

After=network.target remote-fs.target nss-lookup.target

[Service]

ExecStart=/home/zhpt/software/apache-activemq-5.15.2/bin/activemq start

ExecReload=/home/zhpt/software/apache-activemq-5.15.2/bin/activemq restart

ExecStop=/home/zhpt/software/apache-activemq-5.15.2/bin/activemq stop

[Install]

WantedBy=multi-user.target

ExecStart、ExecReload、ExecStop 分别为 服务启动、重启、关闭时执行的脚本。

2、对注册的服务进行操作

#启动服务

[root@localhost ~]# systemctl start activemq.service

#设置开机启动服务

[root@localhost ~]# systemctl enable activemq.service

#取消开机启动服务

[root@localhost ~]# systemctl disenable activemq.service

#重启服务

[root@localhost ~]# systemctl restart activemq.service

#关闭服务

[root@localhost ~]# systemctl stop activemq.service

#查看服务运行状态

[root@localhost ~]# systemctl status activemq.service

Linux系统上,为了创建一个MySQL服务并使其能够在系统启动时自动运行,你需要编辑系统的启动管理程序(如Systemd)。以下是创建`mysql.service`文件的基本步骤: 1. 打开终端(Terminal),首先需要定位到`/etc/systemd/system`目录,如果它不存在,你可以通过`sudo mkdir -p /etc/systemd/system`命令创建。 2. 使用文本编辑器(例如`nano`, `vim`或`mate-text-editor`)创建一个新的服务文件,比如`mysql.service`。输入以下内容,替换`<username>`为你安装MySQL时使用的用户名, `<password>`为密码: ```bash [Unit] Description=MySQL Server After=network.target [Service] User=<username> Group=root ExecStart=/usr/bin/mysqld_safe --skip-grant-tables Restart=always Environment="MYSQL_ROOT_PASSWORD=<password>" # 更改以下行以指定其他配置选项 # Environment="MYSQL_DATABASE=mydb" # Environment="MYSQL_USER=myuser" # Environment="MYSQL_PASSWORD=mypassword" [Install] WantedBy=multi-user.target ``` 这里,我们使用`mysqld_safe`守护进程来运行MySQL,并设置了root用户的临时权限以便于设置初始数据库。 3. 保存并关闭编辑器。如果你使用的是`nano`,按`Ctrl+X`,然后按`Y`确认保存,再按`Enter`。 4. 激活新的服务文件,让Systemd读取它: ```bash sudo systemctl daemon-reload ``` 5. 启动新创建的服务: ```bash sudo systemctl start mysql.service ``` 6. 验证服务是否已成功启动并设置为开机自启: ```bash sudo systemctl status mysql.service sudo systemctl enable mysql.service ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值