linux cron计划任务、chkconfig 命令、systemd命令、unit 相关、target 相关

本文详细介绍Linux系统中定时任务的设置与管理方法,包括crontab的基本使用、计划任务的创建与删除、查看已有的任务等。同时,还介绍了服务的启动、停止及开机自启设置等内容。

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

1、设置说明位置 : cat /etc/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 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# | | | | |
# * * * * * user-name command to be executed

分别所表示的时间用法:

编辑计划任务的命令: crontab -e

0 0 * * * /bin/bash /usr/loacal/sbin/aa.sh >> /var/log/aa.log 2>>/var/log/aa.log

0 0 1-10 */3 * 4,5 /bin/bash /usr/local/sbin/bb.sh >> /var/log/bb.log 2>>/var/log/bb.log

查看有哪些计划任务:crontab -l (当前用户的计划任务); crontab -u root -l  (查看指定用户的计划任务)

若要真正能执行,还需要启动crond 服务: systemctl start crond   (systemctl enable crond  设置为开机启动)

查看是否启动:sytemctl status crond  或 ps -aux | grep "crond"

关闭这个服务:systemctl stop crond

特别注意命令中,最好写命令的绝对路径!

计划任务文件所存放的位置:/etc/var/spool/cron/

若要备份,选择相关的,进行复制就可以了

删除计划任务:crontab -r   (删除当前用户的计划任务); crontab -u root -r  (删除指定用户的计划任务)

 

2、chkconfig  命令:

查看系统里有哪些服务: chkconfig   或 chkconfig --list

设置默认开启某个服务: chkconfig name_service  on

设置默认关闭某个服务: chkconfig name_service  off

这里的0-6分别表示的是:0 关机;  1 单用户;  2多用户没有(nfs文件系统,网络);  3 多用户(包含nfs) 4保留;

5 图形桌面模式;6 重启。 (一般默认为3)

若需要指定某个支行级别下关闭或开启某个服务:

chkconfig  --level # name_service off   (指定在某个级别关闭某服务,--level # 表示某级别、name_service 表示某个服务的名字)

chkconfig --level #  name_servcie  on  (指定在某个级别开启某服务)

增加一个服务到启动列表,或删除一个服务到启动列表:

chkconfig  --add  name_service  (增加一个服务)

chkconfig  --del  name_service   (删除一个服务)

扩展:

chkconfig : ###  ##  ##  (这个是必需需要的)

###: 表示运行级别

##:  开机时的启动顺序

##: 关机时的关闭顺序

3、systemd命令:

查看所有类型(target servcie)的:systemctl list-unit-files 

查看所有服务类型的:systemctl list-units --all --type=service

查看只有启动了的服务类型的:systemctl  list-units  --type=service  (包含启动失败的服务)

systemctl enable name_servcie.servcie (或 systemctl  enable name_service) (设置为开机启动)

systemctl disable name_servcie.servcie (或 systemctl  disable name_service) (设置为开机关闭)

systemctl start  name_servcie.servcie  (开启某服务)

systemctl stop  name_servcie.servcie   (关闭某服务)

systemctl status  name_service   systemctl is-active name_servcie(查看某服务现在的状态)

systemctl is-enabled name_service  (查看某服务是否开机启动)

4、unit 相关命令:

在这个路径下的: /usr/lib/systemd/system 所以文件都是unit文件

service  系统服务

target 多个unit组成的组

device 硬件设备

mount 文件系统的挂载点

automount 自动挂载

path  文件路径相关

scope 非systemd 启动的 外部进程

slice 进程组

snapshot systemd  systemd 的快照

socket  套接字

swap  swap文件

timer   定时器

具体的几个相关命令:(这个在之前systemd 里提到过)

查看所有类型(target servcie)的文件:systemctl list-unit-files 

查看所有服务类型的:systemctl list-units --all --type=service

查看只有启动了的服务类型的:systemctl  list-units  --type=service  (包含启动失败的服务)

systemctl  list-units  查看所有unit

systemctl list-units  --all --state=inactvie  /active

systemctl status  name_service   systemctl is-active name_servcie(查看某服务现在的状态)

 

5、target 相关(target是多个unit的组合):

target 就是unit 的组合 (target 文件可以理解为(多个unit)的快捷方式 或是软连接,然后组合在一起的)

systemctl list-unit-files --type=target  (查看所有的target)

systemctl list-dependencies multi-user.target  (查看target是由哪些unit 组合成的)

systemctl list-dependencies basic.target (查看指定target 由哪些unit组成)

systemctl get-default   (查看默认target)

systemctl set-default multi-user.target   (设置默认的target)

cat  /user/lib/systemd/system/name_service.service  (查看某个服务属于哪个target)

··

转载于:https://www.cnblogs.com/nfyx/p/9027051.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值