linux后台执行命令crontab
有如下几种方式:
crontab
at
&
nohup
1. crontab
定时执行任务
# crontab -e //编辑crontab配置文件
Select an editor. To change later, run 'select-editor'.
1. /bin/ed
2. /bin/nano <---- easiest
3. /usr/bin/emacs24
4. /usr/bin/vim.basic
5. /usr/bin/vim.tiny
Choose 1-5 [2]:
# crontab -l //查看crontab配置文件
# Edit this file to introduce tasks to be run by cron.
#
# Each task to run has to be defined through a single line
# indicating with different fields when the task will be run
# and what command to run for the task
#
# To define the time you can provide concrete values for
# minute (m), hour (h), day of month (dom), month (mon),
# and day of week (dow) or use '*' in these fields (for 'any').#
# Notice that tasks will be started based on the cron's system
# daemon's notion of time and timezones.
#
# Output of the crontab jobs (including errors) is sent through
# email to the user the crontab file belongs to (unless redirected).
#
# For example, you can run a backup of all your user accounts
# at 5 a.m every week with:
# 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/
#
# For more information see the manual pages of crontab(5) and cron(8)
#
# m h dom mon dow command
5 * * * * ls
# crontab -r //删除所有调度任务
语法格式
分 | 小时 | 日 | 月 | 星期 | 命令 |
---|---|---|---|---|---|
0-59 | 0-23 | 1-31 | 1-12 | 0-6 | cmd |
特殊符号
*:所有数字
/:每
-:一个区间
,:多个数字
2. at
特定任务运行一次
# service atd status //查看atd服务
atd start/running, process 1350
# at now + 5 minutes //添加
warning: commands will be executed using /bin/sh
at> /home/a.out
at> ctrl+D
job 4 at Wed Apr 19 14:48:00 2017
时间 | 例子 | 说明 |
---|---|---|
Minute | at now + 5 minutes | 任务在5分钟后运行 |
Hour | at now + 1 hour | 任务在1小时后运行 |
Days | at now + 3 days | 任务在3天后运行 |
Weeks | at now + 2 weeks | 任务在两周后运行 |
Fixed | at midnight | 任务在午夜运行 |
Fixed | at 10:30pm | 任务在晚上10点30分 |
Fixed | at 23:59 12/31/2018 | 任务在2018年12月31号23点59分 |
# at -l //列出所有作业
1 Thu Apr 20 02:05:00 2017 a thomas
# at -r job //删除作业
Warning: deleting running job
3. &
在后台运行一个占用时间不长的任务
# jobs //查看后台运行任务
# fg //将后台运行任务调至前台
4. ctrl + z
将一个正在前台执行的命令放到后台,并且处于暂停状态
5. nohup
在后台运行一个命令,即使在用户退出时也不受影响
# nohup --help
Usage: nohup COMMAND [ARG]...
or: nohup OPTION
6. 查看当前终端的进程id
# echo $$
2602
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!