Linux中可使用
crontab -l
命令查看当前用户的任务计划,用`ls /etc/cron.
Linux 系统中,查看任务计划通常涉及检查系统的定时任务调度工具,如 cron
,这些任务可以由系统管理员或普通用户设置,用于在特定时间或间隔执行命令和脚本,以下是详细的步骤和方法,帮助你在 Linux 系统中查找和管理任务计划。
使用 crontab
查看当前用户的任务计划
crontab
是每个用户用于管理个人定时任务的文件,要查看当前用户的任务计划,可以使用以下命令:
crontab -l
输出示例:
# Example of job definition:
# .---------------minute (0 59)
# | .------------hour (0 23)
# | | .---------day of month (1 31)
# | | | .------month (1 12) OR jan,feb,mar,apr ...
# | | | | .---day of week (0 7) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# | | | | |
# * * * * * user-name command to be executed
0 5 * * * /usr/bin/backup.sh
30 2 * * * /usr/bin/cleanup.sh
说明:
crontab -l
返回空内容,表示当前用户没有设置任何任务计划。- 如果需要编辑任务计划,可以使用
crontab -e
命令。
查看系统级任务计划(/etc/crontab
)
系统级任务计划存储在 /etc/crontab
文件中,它定义了全局的定时任务,使用以下命令查看:
cat /etc/crontab
输出示例:
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
# For details see man 4 crontab
# Example of job definition:
# .---------------minute (0 59)
# | .------------hour (0 23)
# | | .---------day of month (1 31)
# | | | .------month (1 12) OR jan,feb,mar,apr ...
# | | | | .---day of week (0 7) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# | | | | |
# * * * * * user-name command to be executed
0 0 * * * root /usr/bin/system_backup.sh
说明:
/etc/crontab
文件包含系统级任务计划,通常由 root 用户管理。- 文件的前几行定义了环境变量(如
SHELL
、PATH
)和邮件接收者(MAILTO
)。
查看其他用户的 crontab
如果需要查看其他用户的任务计划,可以使用以下命令(需要 root 权限):
sudo crontab -u username -l
说明:
- 将
username
替换为目标用户的用户名。 - 此命令会显示指定用户的
crontab
查看 cron.d
目录中的任务计划
系统级的定时任务还可以存储在 /etc/cron.d
目录中,使用以下命令查看:
ls /etc/cron.d
输出示例:
at.jobs mysql-cron update-notifier
说明:
/etc/cron.d
目录中的文件通常是系统服务或应用程序的定时任务。- 可以使用
cat /etc/cron.d/filename
查看具体内容。
查看 cron.daily
、cron.hourly
等目录
某些系统任务会存储在 /etc/cron.daily
、/etc/cron.hourly
、/etc/cron.weekly
和 /etc/cron.monthly
目录中,这些目录中的脚本会按照相应的频率执行,使用以下命令查看:
ls /etc/cron.daily
输出示例:
apt-compat logrotate man-db.gz
说明:
- 这些目录中的脚本通常由系统级的
cron
任务调用。 /etc/cron.daily
中的脚本会在每天执行一次。
使用 ps
或 top
查看正在运行的定时任务
如果需要查看当前正在运行的定时任务进程,可以使用 ps
或 top
命令。
ps aux | grep cron
输出示例:
root 1234 0.0 0.1 12345 1234 ? Ss 08:00 0:00 /usr/sbin/cron -f
说明:
- 该命令会显示与
cron
相关的进程。 /usr/sbin/cron -f
表示cron
守护进程正在前台运行。
使用 systemctl
检查 cron
服务状态
如果怀疑 cron
服务未正常运行,可以使用 systemctl
检查其状态:
systemctl status cron
输出示例:
● cron.service Regular background program processing daemon
Loaded: loaded (/lib/systemd/system/cron.service; enabled; vendor preset: enabled)
Active: active (running) since Wed 2023-10-01 08:00:00 CST; 1h ago
Main PID: 1234 (cron)
Tasks: 0 (limit: 4915)
Memory: 1.2M
CGroup: /system.slice/cron.service
└─1234 /usr/sbin/cron -f
说明:
cron
服务未运行,可以使用systemctl start cron
启动它。- 如果需要启用开机自启,可以使用
systemctl enable cron
。
小编总结任务计划的位置
以下是 Linux 系统中任务计划的主要位置汇总:
任务类型 | 文件或目录 | 说明 |
---|---|---|
当前用户的任务计划 | crontab -e |
使用 crontab -l 查看 |
系统级任务计划 | /etc/crontab |
全局任务,通常由 root 管理 |
其他用户的任务计划 | sudo crontab -u [username] -l |
查看指定用户的任务计划 |
系统级任务脚本 | /etc/cron.d/ |
系统服务或应用程序的定时任务 |
定时任务脚本 | /etc/cron.daily 、/etc/cron.hourly |
按频率执行的脚本 |
cron 服务状态 |
systemctl status cron |
检查 cron 服务是否正常运行 |
FAQs
Q1: crontab -l
没有输出,是否表示没有任务计划?
A1: 是的,crontab -l
没有输出,表示当前用户没有设置任何任务计划,你可以通过 crontab -e
添加新的任务计划。
Q2: 如何删除当前用户的所有任务计划?
A2: 你可以使用以下命令清空当前用户的 crontab
:
crontab -r
小伙伴们,上文介绍linux如何查任务计划的内容,你了解清楚吗?希望对你有所帮助,任何问题可以给我留言,让我们下期再见吧。
原创文章,发布者:酷番叔,转转请注明出处:https://cloud.kd.cn/ask/10293.html