linux任务计划、chkconfig工具、systemd、unit和target介绍

linux任务计划cron

cat /etc/crontab 配置文件

  • crontab -u -e//定义 -l -r
  • 格式:分 时 日 月 周 user command

  • 文件/var/spool/cron/username

  • 分范围0-59,时范围0-23,日范围0-31,月范围0-12,周1-6
  • 可用格式1-5表示一个范围1到5
  • 可用格式1,2,3,表示1或者2或者3
  • 可用格式*/2表示被2整除的数字,比如小时,那就是每隔2小时
  • 要保证服务是启动状态
    systemctl start crond  //启动服务
    ps aux |grep cron   //查看进程,表示服务启动
    #或者查看状态,显示绿色表示启动
    systemctl stop crond  //停掉服务的命令
  • systemctl start crond.service

chkconfig工具

  • chkconfig --list //列出所有的服务来
  • chkconfig --level 3 network off
  • chkconfig --level 325 network off
  • chkconfig --del network
  • chkconfig --add network

systemd管理服务

  • systemctl list-units --all --type=service
  • 几个常用服务相关的命令
    • systemctl enable crond.service //让服务开机启动
    • systemctl disable crond //不让开机启动
    • systemctl status crond //查看状态
    • systemctl stop crond //停止服务
    • systemctl start crond //启动服务
    • systemctl restart crond //重启服务
    • systemctl is-enabled crond //检查服务是否开机启动

unit介绍

  • ls /usr/lib/systemd/system //系统所有unit,分为以下类型
  • service 系统服务
  • target 多个unit组成的组
  • device 硬件设备
  • mount 文件系统挂载点
  • automount 自动挂载点
  • path 文件或路径
  • scope 不是由systemd 启动的外部进程
  • slice 进程组
  • snapshot systemd快照
  • socket 进程间通信套接字
  • swap swap文件
  • timer 定时器
  • unit 相关的命令
    • systemctl list-units //列出正在运行的unit
    • systemctl list-units --all //列出所有,包括失败的或者inactive的
    • systemctl list-units --all --state=inactive //列出inactive的unit
    • systemctl list-units --type=service //列出状态为active的service
    • systemctl is-active crond.service //查看某个服务是否为active

target介绍

  • 系统为了方便管理用target来管理unit
  • systemctl list-unit-files --type=target
  • systemctl list-dependencies multi-usrt.target //查看指定target下面有哪些unit
  • systemctl get-default //查看系统默认的target
  • systemctl set-default multi-user.target
  • 一个service属于一种类型的unit
  • 多个unit组成了一个target
  • 一个target里面包含了多个service
  • cat /usr/lib/systemd/system/sshd.service //看[install]部分

转载于:https://blog.51cto.com/3622288/2047490

### 回答1: 要设置Linux系统中Redis开机自启动,可以通过编写一个自定义的systemd服务单元来实现,不需要使用chkconfig命令。以下是具体的步骤: 1. 创建Redis systemd服务单元的配置文件。打开一个文本编辑器,例如vi或nano,并创建一个名为redis.service的文件。 ```shell sudo vi /etc/systemd/system/redis.service ``` 2. 在redis.service文件中粘贴以下内容: ```shell [Unit] Description=Redis Server After=network.target [Service] ExecStart=/usr/local/bin/redis-server /etc/redis.conf ExecStop=/usr/local/bin/redis-cli shutdown User=redis Group=redis Restart=always [Install] WantedBy=multi-user.target ``` 请确保ExecStart的路径和参数与你的实际配置相匹配。上面的示例假定Redis的可执行文件为/usr/local/bin/redis-server,配置文件为/etc/redis.conf。 3. 保存并关闭文件。 4. 启用Redis服务。输入以下命令以启用自定义的Redis服务: ```shell sudo systemctl enable redis.service ``` 5. 启动Redis服务。输入以下命令以启动Redis服务: ```shell sudo systemctl start redis.service ``` 现在,Redis将在系统启动时自动启动。你可以使用以下命令来验证它的状态: ```shell sudo systemctl status redis.service ``` 该命令将显示Redis服务的当前状态信息。 通过编写自定义的systemd服务单元,你可以实现Linux系统中Redis开机自启动,而无需使用chkconfig命令。 ### 回答2: 要在Linux上设置Redis开机自启动,而不使用chkconfig,可以使用以下步骤: 1. 打开终端并切换到root用户。 2. 进入Redis安装目录,通常为"/usr/local/redis"。 3. 在该目录下创建一个名为"redis.service"的文件,可以使用vi或任何文本编辑器进行创建。 4. 在"redis.service"文件中输入以下内容: ``` [Unit] Description=Redis service After=network.target [Service] ExecStart=/usr/local/redis/bin/redis-server /usr/local/redis/redis.conf ExecStop=/usr/local/redis/bin/redis-cli shutdown [Install] WantedBy=default.target ``` 上述配置中,需要根据实际安装路径和redis.conf文件位置进行修改。 5. 保存并关闭文件。 6. 运行以下命令将该服务加入到系统服务中: ``` systemctl enable /usr/local/redis/redis.service ``` 7. 接下来,即可使用以下命令来启动、停止或重新启动Redis服务: ``` systemctl start redis systemctl stop redis systemctl restart redis ``` 这样,Redis将会在系统启动时自动启动,并且可以使用systemctl命令来管理Redis服务。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值