linux shell 实现定时每天早上8点执行一条特定命令
时间: 2023-08-11 12:23:50 浏览: 57
可以使用 Linux 自带的 cron 工具来实现定时任务。
首先,打开终端,输入以下命令打开 crontab 编辑器:
```bash
crontab -e
```
然后在编辑器中添加以下内容:
```bash
0 8 * * * /path/to/command
```
其中,`0 8 * * *` 表示每天的早上 8 点执行,`/path/to/command` 表示要执行的命令路径。
例如,如果要每天早上 8 点执行 `/home/user/test.sh` 脚本,就可以在 crontab 编辑器中添加以下内容:
```bash
0 8 * * * /home/user/test.sh
```
最后,保存并退出编辑器即可。现在,每天早上 8 点时,Linux 就会自动执行 `/home/user/test.sh` 脚本了。
相关问题
linux shell 实现定时每天早上8点35分执行一条特定命令
同样可以使用 Linux 自带的 cron 工具来实现定时任务。
首先,打开终端,输入以下命令打开 crontab 编辑器:
```bash
crontab -e
```
然后在编辑器中添加以下内容:
```bash
35 8 * * * /path/to/command
```
其中,`35 8 * * *` 表示每天的早上 8 点 35 分执行,`/path/to/command` 表示要执行的命令路径。
例如,如果要每天早上 8 点 35 分执行 `/home/user/test.sh` 脚本,就可以在 crontab 编辑器中添加以下内容:
```bash
35 8 * * * /home/user/test.sh
```
最后,保存并退出编辑器即可。现在,每天早上 8 点 35 分时,Linux 就会自动执行 `/home/user/test.sh` 脚本了。
定时执行一条命令的shell脚本
#!/bin/bash
# 每天的凌晨1点执行一条命令的脚本
# crontab -e 添加如下定时任务:0 1 * * * /bin/bash /path/to/script.sh
# 要执行的命令
command_to_execute="echo 'Hello, World!'"
# 执行命令
eval $command_to_execute
# 记录日志
echo "Command executed at $(date)" >> /var/log/command.log
# 结束脚本
exit 0