Linux之服务和计划任务

服务:

在后台运行的软件就叫服务
参数:
start
stop
restart

apache启动后默认降权

service httpd start实际调用的是/etc/init.d/httpd这个shell脚本
而/etc/init.d是软连接文件夹,实际在/etc/rc.d/init.d
[wyb@localhost ~]$ ls -al /etc/init.d
lrwxrwxrwx. 1 root root 11 Apr 11 02:54 /etc/init.d -> rc.d/init.d

添加服务:

将脚本放在/etc/init.d/下即可

若要自启则:

chkconfig --list   //查看chk管理的自启动项
chkconfig --add httpd   添加到chk管理里面
chkconfig --del httpd   将httpd移出chk管理
chkcofnig --level 35 httpd on  init的345将开启
chkcofnig --level 345 httpd off
/etc/rc.d/rc3.d和rc5.d里将有httpd

计划任务:

启动方式:
系统启动时将有一个脚本将cron服务开启,启动后cron命令会搜索全局型配置文件/etc/crontab和位于/var/spool/corn目录下以用户名命名的文件

cron:每分钟醒来依次查看当前是否有需要运行的命令,最小单位就是分钟

添加任务:

格式:分(0-59) 时 日 月 周(0-6) 命令

第一种方法:
crontab -e  //任何权限都可以运行这个命令,针对某个用户

[wyb@localhost spool]$ crontab -e   //进入编辑,实际是写入到/var/spool/cron/username这个文件中
no crontab for wyb - using an empty one
*/1 * * * * date >> /tmp/date.txt   //每一分钟都向date.txt写入
1 * * * * date >> /tmp/date1.txt        //在第一分钟时将数据写入
~
~
~
"/tmp/crontab.T8sDqC" 1L, 34C written
crontab: installing new crontab
[wyb@localhost spool]$ sudo ls /var/spool/cron/
wyb
[wyb@localhost spool]$ sudo cat /var/spool/cron/wyb
*/1 * * * * date >> /tmp/date.txt
1 * * * * date >> /tmp/date1.txt
[wyb@localhost spool]$ 

第二种方法:
编辑/etc/crontab文件    //要具有root权限,针对系统任务
vim /etc/crontab会以root权限执行,不推荐使用
01 * * * * 用户名 命令 date >> /tmp/date2.txt在每个小时的第一分钟执行

查看用户任务:

[wyb@localhost tmp]$ sudo cat /var/spool/cron/wyb   //用户的计划任务
*/1 * * * * date >> /tmp/date.txt
[wyb@localhost tmp]$ sudo tail -5 /etc/crontab   //系统计划任务
# |  |  |  .------- 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
*/1 * * * * wyb date >> /tmp/wyb.txt
[wyb@localhost tmp]$ crontab -l   //只能显示普通用户定义的任务
*/1 * * * * date >> /tmp/date.txt
[wyb@localhost tmp]$ crontab -r   //移除用户所有计划任务,不能移除系统定的计划任务,即不能移除在/etc/crontab中的任务
[wyb@localhost tmp]$ crontab -l
no crontab for wyb
[wyb@localhost tmp]$ sudo tail -3 /etc/crontab 
# |  |  |  |  |
# *  *  *  *  * user-name command to be executed
*/1 * * * * wyb date >> /tmp/wyb.txt
[wyb@localhost tmp]$ 
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • Linux 系统提供了使用者控制计划任务的命令 :crontab 命令。 一、crond简介 crond是linu...
    happeace阅读 1,947评论 0 53
  • Linux定时任务Crontab命令详解 linux 系统则是由 cron (crond) 这个系统服务来控制的。...
    孙燕超阅读 1,931评论 0 4
  • 简介:Crontab命令常见于Unix和类Unix的操作系统之中,用于设置周期性被执行的指令。本课程通过对Cron...
    Amisuer阅读 4,873评论 0 1
  • 前一天学习了 at 命令是针对仅运行一次的任务,循环运行的例行性计划任务,linux系统则是由 cron (cro...
    Zcclucky阅读 641评论 0 0
  • 想你的美 花瓣飘落想起你了 如今你在何方,又在思念着谁呢? 回忆从前的时光 云朵飘飘游走,有蓝天的陪伴你不孤单 鲜...
    诺一梦阅读 470评论 0 0

友情链接更多精彩内容