linux的service文件中如何写ExecStart能够启动多个应用程序
时间: 2023-04-02 16:04:14 浏览: 271
你可以在ExecStart后面写多个命令,用分号隔开,例如:
ExecStart=/usr/bin/command1; /usr/bin/command2; /usr/bin/command3
这样就可以启动多个应用程序了。
相关问题
linux下设置程序开机自启动
在 Linux 下,有多种方式可以设置程序开机自启动,以下是其中的两种方法:
1. 使用系统自带的服务管理工具
大多数 Linux 系统都自带了服务管理工具,例如 Ubuntu 下的 systemd。可以通过以下步骤设置程序开机自启动:
1. 编写一个服务文件,通常以 `.service` 结尾,比如 `myservice.service`。
2. 将服务文件拷贝到 `/etc/systemd/system/` 目录下。
3. 执行 `systemctl daemon-reload` 命令,重新加载服务文件。
4. 执行 `systemctl enable myservice.service` 命令,启用服务。
示例服务文件 `myservice.service`:
```
[Unit]
Description=My Service
After=network.target
[Service]
Type=simple
ExecStart=/path/to/my/program
[Install]
WantedBy=multi-user.target
```
该服务文件定义了一个名为 My Service 的服务,它依赖于网络服务,在网络服务启动之后才会启动。服务类型为 simple,执行的命令是 `/path/to/my/program`。最后,将该服务安装为多用户模式下的服务。
2. 使用 rc.local 文件
rc.local 文件是一个在系统启动时执行的脚本文件。可以通过以下步骤设置程序开机自启动:
1. 编辑 `/etc/rc.local` 文件,添加需要启动的程序命令。
2. 将程序命令添加到文件中,格式为 `<command> &`,例如 `/path/to/my/program &`。
3. 保存文件并退出。
示例 `/etc/rc.local` 文件:
```
#!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.
/path/to/my/program &
exit 0
```
该文件在系统启动时会执行 `/path/to/my/program` 命令,并加上后台运行符号 `&`,使程序在后台运行。最后,将文件保存并设置执行权限即可。
生成liunx service 开机自动启动程序
1. 创建一个shell脚本
首先,我们需要编写一个启动自己程序的shell脚本,比如我们的程序是/home/user/myapp/myapp,那么我们可以在/home/user/myapp/目录下创建一个start.sh文件,文件内容如下:
#!/bin/bash
cd /home/user/myapp/
./myapp
注意,要给start.sh文件赋予可执行权限:
chmod +x /home/user/myapp/start.sh
2. 创建一个systemd服务文件
接下来,我们需要创建一个systemd服务文件,这个文件告诉systemd如何启动我们的程序,并在开机自动启动。在/etc/systemd/system/目录下创建一个myapp.service文件,文件内容如下:
[Unit]
Description=My App Service
After=network.target
[Service]
Type=simple
User=user
WorkingDirectory=/home/user/myapp/
ExecStart=/home/user/myapp/start.sh
Restart=always
[Install]
WantedBy=multi-user.target
说明:
- Description:服务的描述信息。
- After:告诉systemd这个服务必须在network.target启动之后启动。
- Type:启动程序的类型,这里是simple,表示直接执行ExecStart指定的脚本。
- User:指定服务运行的用户。
- WorkingDirectory:指定启动程序的工作目录,这里是/home/user/myapp/。
- ExecStart:指定启动程序的命令,这里是/home/user/myapp/start.sh。
- Restart:当程序意外退出时,自动重启。
- WantedBy:告诉systemd这个服务应该在哪个target中启动,这里是multi-user.target,表示多用户模式下启动。
3. 启动服务
完成上面两步后,我们就可以启动这个服务了:
systemctl start myapp.service
4. 设置开机自启动
为了让这个服务在开机时自动启动,我们需要运行:
systemctl enable myapp.service
这样,当我们下次重启系统时,这个服务就会自动启动了。
5. 查看服务状态
我们可以随时查看服务的状态:
systemctl status myapp.service
也可以查看服务的日志:
journalctl -u myapp.service
到此为止,我们就成功地将我们的程序设置为了开机自启动的Linux服务。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)