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
原文地址:https://www.cnblogs.com/zhangxuechao/p/11709734.html