Linux Systemd CrontabʵÀý½Ì³Ì£ºÔõÑù׼ʱÕûÀíϵͳÈÕÖ¾
Linux Systemd CrontabʵÀý½Ì³Ì£ºÔõÑù׼ʱÕûÀíϵͳÈÕÖ¾
Ò»¡¢¼ò½é
ÔÚLinuxϵͳÖУ¬ÏµÍ³ÈÕÖ¾ÎļþÊǼͼϵͳÔËÐÐ״̬¡¢¹ýʧÒÔ¼°ÆäËûÖ÷ÒªÐÅÏ¢µÄÒªº¦ÎļþÖ®Ò»¡£Ëæ×Åʱ¼äµÄÍÆÒÆ£¬ÏµÍ³ÈÕÖ¾Îļþ»áÔ½À´Ô½´ó£¬Õ¼Óôó×Ú´ÅÅ̿ռ䡣ΪÁ˼á³ÖϵͳµÄÕý³£ÔËÐУ¬°´ÆÚÕûÀíϵͳÈÕÖ¾ÊÇÊ®·ÖÐëÒªµÄ¡£±¾ÎĽ«ÏêϸÏÈÈÝÔõÑùʹÓÃSystemd Crontab׼ʱÕûÀíϵͳÈÕÖ¾£¬Í¬Ê±ÌṩÏêϸµÄ´úÂëʾÀý¡£
¶þ¡¢Ê¹ÓÃSystemd Crontab׼ʱִÐо籾
Systemd CrontabÊÇ»ùÓÚLinuxϵͳµÄЧÀÍÖÎÀí¹¤¾ß£¬¿ÉÒÔÓÃÀ´×¼Ê±Ö´ÐÐʹÃü¡£ÏÂÃæÊÇʹÓÃSystemd Crontab׼ʱִÐо籾µÄÏêϸ°ì·¨£º
½¨ÉèÕûÀíÈÕÖ¾µÄ¾ç±¾
Ê×ÏÈ£¬ÎÒÃÇÐèÒª½¨ÉèÒ»¸ö½ÅÔÀ´ÕûÀíϵͳÈÕÖ¾¡£¿ÉÒÔʹÓÃshell¾ç±¾ÓïÑÔÀ´±àд£¬ÒÔÏÂÊÇÒ»¸öʾÀýÕûÀíÈÕÖ¾µÄ¾ç±¾£¨clean_logs.sh£©£º
#!/bin/bash # ÕûÀí/var/logĿ¼ÏµÄϵͳÈÕÖ¾Îļþ find /var/log -type f -name "*.log" -exec rm -rf {} ; # ÖØÆôϵͳÈÕ־ЧÀÍ systemctl restart rsyslog
µÇ¼ºó¸´ÖÆ
ÉÏÊö¾ç±¾Ê¹ÓÃfindÏÂÁîÕÒµ½/var/logĿ¼ÏµÄËùÓÐÈÕÖ¾Îļþ£¬²¢Ê¹ÓÃrmÏÂÁîɾ³ýÕâЩÎļþ¡£×îºó£¬Ê¹ÓÃsystemctlÏÂÁîÖØÐÂÆô¶¯rsyslogЧÀÍ£¬ÒÔÈ·±£ÏµÍ³ÈÕÖ¾¹¦Ð§Õý³£¡£
½¨ÉèSystemdЧÀ͵¥Î»Îļþ
½ÓÏÂÀ´£¬ÎÒÃÇÐèÒª½¨ÉèÒ»¸öSystemdЧÀ͵¥Î»Îļþ£¬ÓÃÓÚ½ç˵ҪִÐеľ籾¡£½¨ÉèÒ»¸öÃûΪclean_logs.serviceµÄÎļþ£¬ÄÚÈÝÈçÏ£º
[Unit] Description=Clean system logs [Service] ExecStart=/path/to/clean_logs.sh [Install] WantedBy=default.target
µÇ¼ºó¸´ÖÆ
ÐèÒª½«ExecStartµÄ·¾¶ÐÞ¸ÄΪÏÖʵµÄ¾ç±¾Â·¾¶¡£
½¨ÉèSystemd CrontabÉèÖÃÎļþ
ÔÚ/etc/systemd/system/Ŀ¼Ï½¨ÉèÒ»¸öÃûΪclean_logs.timerµÄSystemd CrontabÉèÖÃÎļþ£¬ÄÚÈÝÈçÏ£º
[Unit] Description=Run clean_logs.service every day [Timer] OnCalendar=daily Persistent=true [Install] WantedBy=timers.target
µÇ¼ºó¸´ÖÆ
ÉÏÊöÉèÖÃÎļþÖУ¬OnCalendarÉèÖÃÁË׼ʱʹÃüµÄÖ´ÐÐʱ¼ä£¬ÕâÀïÉèÖÃΪÌìÌìÖ´ÐÐÒ»´Î¡£PersistentÉèÖÃΪtrueÌåÏÖ×ÝȻϵͳ¹Ø»ú£¬Ê¹ÃüÒ²»áÔÚÏ´ÎÆô¶¯Ê±¼ÌÐøÖ´ÐС£
Æô¶¯ºÍÖÎÀí׼ʱʹÃü
Íê³ÉÒÔÉÏ°ì·¨ºó£¬¿ÉÒÔƾ֤ÒÔÏÂÏÂÁîÀ´Æô¶¯ºÍÖÎÀí׼ʱʹÃü£º
Æô¶¯×¼Ê±Ê¹Ãü£ºsystemctl start clean_logs.timer
×èֹ׼ʱʹÃü£ºsystemctl stop clean_logs.timer
ÖØÆô׼ʱʹÃü£ºsystemctl restart clean_logs.timer
Éó²é׼ʱʹÃü״̬£ºsystemctl status clean_logs.timer
Èý¡¢Éó²é׼ʱʹÃüÖ´ÐÐЧ¹û
׼ʱʹÃüÖ´ÐÐÍê³Éºó£¬ÎÒÃÇÏ£ÍûÄܹ»Éó²éʹÃüµÄÖ´ÐÐЧ¹û£¬ÒÔÈ·ÈÏϵͳÈÕÖ¾ÊÇ·ñ±»ÕûÀíÀֳɡ£¿ÉÒÔͨ¹ýÒÔÏÂÁ½ÖÖ·½·¨À´Éó²é£º
Éó²éSystemd CrontabÈÕÖ¾£ºjournalctl -u clean_logs.timer
Éó²éϵͳÈÕÖ¾Îļþ£ºtail -n 100 /var/log/syslog
ËÄ¡¢×ܽá
ʹÓÃSystemd Crontab¿ÉÒÔÀû±ãµØ׼ʱÕûÀíϵͳÈÕÖ¾£¬×èÖ¹´ÅÅ̿ռ䱻¹ý´óµÄÈÕÖ¾ÎļþÕ¼Óᣱ¾ÎÄͨ¹ýÏêϸµÄ´úÂëʾÀý£¬ÏêϸÏÈÈÝÁËÔõÑùʹÓÃSystemd CrontabÀ´×¼Ê±ÕûÀíϵͳÈÕÖ¾¡£Ï£Íû±¾ÎÄÄܹ»¶ÔÄúÓÐËù×ÊÖú£¬Ê¹ÄúÄܹ»¸üºÃµØÖÎÀíLinuxϵͳµÄÈÕÖ¾Îļþ¡£
ÒÔÉϾÍÊÇLinux Systemd CrontabʵÀý½Ì³Ì£ºÔõÑù׼ʱÕûÀíϵͳÈÕÖ¾µÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡