ubuntu开机自动运行命令
时间: 2024-10-20 08:18:50 浏览: 79
Ubuntu系统中,如果你想让某个命令在启动时自动运行,你可以将它添加到系统的启动脚本或初始化脚本中。以下是几种常见的方法:
1. **Systemd**: Ubuntu 16.04及更高版本使用systemd作为默认的服务管理器。创建一个.service文件并放入/etc/systemd/system/目录下,例如创建名为"myservice.service"的文件,内容类似:
```bash
[Unit]
Description=My Service
[Service]
ExecStart=/path/to/my/command
Restart=always
User=your_username
[Install]
WantedBy=multi-user.target
```
然后运行`sudo systemctl daemon-reload`更新服务列表,最后启用`sudo systemctl start myservice`并设置开机自启 `sudo systemctl enable myservice`.
2. **Upstart**: 这个老版本的启动系统在某些较旧的Ubuntu上仍然可用。在/etc/init/目录下创建一个配置文件,如myprogram.conf,同样包含执行命令。不过,systemd逐渐取代了upstart,所以对于新系统,推荐使用systemd。
3. **cron job**: 如果你需要在每次用户登录后运行一次,可以设置cron job。编辑crontab文件(`crontab -e`),添加类似这样的行:
```
@reboot /path/to/my/command
```
记得保存并关闭文件。
阅读全文
相关推荐


















