linux后台执行命令crontab

本文介绍了在Linux环境下如何使用crontab、at、&、nohup等工具进行定时任务及后台任务的管理。通过实例展示了如何设置周期性任务、一次性任务,并提供了后台运行命令的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

有如下几种方式:
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-590-231-311-120-6cmd

特殊符号

*:所有数字
/:每
-:一个区间
,:多个数字

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
时间例子说明
Minuteat now + 5 minutes任务在5分钟后运行
Hourat now + 1 hour任务在1小时后运行
Daysat now + 3 days任务在3天后运行
Weeksat now + 2 weeks任务在两周后运行
Fixedat midnight任务在午夜运行
Fixedat 10:30pm任务在晚上10点30分
Fixedat 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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值