ÔõÑùʹÓÃSystemdºÍCrontabÔÚLinuxϵͳÖÐ׼ʱִÐо籾
ÎÊÌ⣺ʹÓÃSystemdºÍCrontabÔÚLinuxϵͳÖÐ׼ʱִÐо籾
ÕýÎÄ£º
ÔÚLinuxϵͳÖУ¬ÎÒÃǾ³£ÐèҪ׼ʱִÐÐһЩ¾ç±¾Ê¹Ãü£¬ºÃ±È±¸·ÝÊý¾Ý¡¢ÕûÀíÈÕÖ¾µÈ¡£±¾ÎĽ«ÏÈÈÝÔõÑùʹÓÃSystemdºÍCrontabÁ½ÖÖ·½·¨À´ÊµÏÖ׼ʱִÐо籾µÄ¹¦Ð§£¬²¢ÌṩÏêϸµÄ´úÂëʾÀý¡£
Ò»¡¢Ê¹ÓÃSystemd׼ʱִÐо籾
SystemdÊÇLinuxϵͳϵÄÒ»¸ö³õʼ»¯ÏµÍ³ºÍЧÀÍÖÎÀíÆ÷£¬Í¨¹ý½¨ÉèSystemd׼ʱʹÃü£¬ÎÒÃÇ¿ÉÒÔÀû±ãµØ׼ʱִÐо籾¡£
½¨ÉèÒ»¸ö.serviceÎļþ£¬ÓÃÓÚ½ç˵׼ʱʹÃüµÄÏà¹Ø²ÎÊý¡£ÒÔ׼ʱÕûÀíÈÕ־ΪÀý£¬¿ÉÒÔ½¨ÉèÒ»¸öÃûΪcleanuplog.serviceµÄÎļþ¡£
[Unit] Description=Cleanup Log Service [Service] Type=simple ExecStart=/path/to/cleanuplog.sh [Install] WantedBy=default.target
µÇ¼ºó¸´ÖÆ
ÆäÖУ¬ExecStartÖ¸¶¨ÁËÒªÖ´Ðеľ籾·¾¶£¬ÐèҪƾ֤ÏÖÕæÏàÐÎÐ޸ġ£ÉúÑIJ¢¹Ø±ÕÎļþ¡£
½¨ÉèÒ»¸ö.timerÎļþ£¬ÓÃÓÚ½ç˵׼ʱʹÃüµÄ´¥·¢Ìõ¼þºÍÖ´ÐÐÂß¼¡£ÒÔÌìÌìÆÆÏþ1µãÖ´ÐÐΪÀý£¬½¨ÉèÒ»¸öÃûΪcleanuplog.timerµÄÎļþ¡£
[Unit] Description=Cleanup Log Timer [Timer] OnCalendar=*-*-* 01:00:00 [Install] WantedBy=timers.target
µÇ¼ºó¸´ÖÆ
ÆäÖУ¬OnCalendarÖ¸¶¨ÁË׼ʱʹÃüµÄ´¥·¢Ê±¼ä£¬ÕâÀïÊÇÌìÌìÆÆÏþ1µã¡£ÉúÑIJ¢¹Ø±ÕÎļþ¡£
½«Á½¸öÎļþÒƶ¯µ½/etc/systemd/systemĿ¼Ï¡£
$ sudo mv cleanuplog.service /etc/systemd/system $ sudo mv cleanuplog.timer /etc/systemd/system
µÇ¼ºó¸´ÖÆ
ÆôÓò¢Æô¶¯×¼Ê±Ê¹Ãü¡£
$ sudo systemctl daemon-reload $ sudo systemctl enable cleanuplog.timer $ sudo systemctl start cleanuplog.timer
µÇ¼ºó¸´ÖÆ
ÏÖÔÚ£¬×¼Ê±Ö´Ðо籾µÄʹÃü¾ÍÒѾ½¨Éè²¢Æô¶¯ÀÖ³ÉÁË¡£
¶þ¡¢Ê¹ÓÃCrontab׼ʱִÐо籾
CrontabÊÇÒ»ÖÖÓÃÓÚÖÜÆÚÐÔÖ´ÐÐÏÂÁî»ò¾ç±¾µÄ¹¤¾ß£¬Í¨¹ý±à¼CrontabÎļþ£¬ÎÒÃÇ¿ÉÒÔÇáËɶ¨Öƶ©Ê±Ö´Ðо籾µÄÐèÇó¡£
±à¼Ä¿½ñÓû§µÄCrontabÎļþ¡£
$ crontab -e
µÇ¼ºó¸´ÖÆ
ÔÚ·¿ªµÄÎļþÖÐÌí¼Ó׼ʱִÐо籾µÄÏÂÁî¡£ÒÔÌìÌìÆÆÏþ2µãÖ´ÐÐΪÀý£¬¿ÉÒÔÌí¼ÓÈçÏÂÄÚÈÝ¡£
0 2 * * * /path/to/cleanuplog.sh
µÇ¼ºó¸´ÖÆ
ÆäÖУ¬0 2 * * *ÌåÏÖÌìÌìÆÆÏþ2µã£¬/path/to/cleanuplog.shÌåÏÖÒªÖ´Ðеľ籾·¾¶£¬ÐèҪƾ֤ÏÖÕæÏàÐÎÐ޸ġ£
ÉúÑIJ¢¹Ø±ÕÎļþ¡£
ÏÖÔÚ£¬×¼Ê±Ö´Ðо籾µÄʹÃü¾ÍÌí¼ÓÀÖ³ÉÁË¡£
×ܽ᣺
±¾ÎÄÏÈÈÝÁËÔÚLinuxϵͳÖÐʹÓÃSystemdºÍCrontabÁ½ÖÖ·½·¨À´×¼Ê±Ö´Ðо籾µÄÒªÁ죬²¢ÌṩÁËÏêϸµÄ´úÂëʾÀý¡£Æ¾Ö¤ÏÖʵÐèÇó£¬Ñ¡ÔñºÏÊʵķ½·¨À´ÊµÏÖ׼ʱʹÃü£¬¿ÉÒÔÌá¸ßÊÂÇéЧÂʺÍ×Ô¶¯»¯ÖÎÀíÄÜÁ¦¡£Ï£Íû±¾ÎÄÄܹ»×ÊÖú¶ÁÕßѧϰºÍÓ¦ÓÃ׼ʱִÐо籾µÄÒªÁì¡£
ÒÔÉϾÍÊÇÔõÑùʹÓÃSystemdºÍCrontabÔÚLinuxϵͳÖÐ׼ʱִÐо籾µÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡