×ðÁú¿­Ê±¹ÙÍøµÇ¼

ÔõÑùʹÓÃ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ϵͳÖÐ׼ʱִÐо籾µÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡

ÃâÔð˵Ã÷£ºÒÔÉÏչʾÄÚÈÝȪԴÓÚÏàÖúýÌå¡¢ÆóÒµ»ú¹¹¡¢ÍøÓÑÌṩ»òÍøÂçÍøÂçÕûÀí£¬°æȨÕùÒéÓë±¾Õ¾Î޹أ¬ÎÄÕÂÉæ¼°¿´·¨Óë¿´·¨²»´ú±í×ðÁú¿­Ê±¹ÙÍøµÇ¼ÂËÓÍ»úÍø¹Ù·½Ì¬¶È£¬Çë¶ÁÕß½ö×ö²Î¿¼¡£±¾ÎĽӴýתÔØ£¬×ªÔØÇë˵Ã÷À´ÓÉ¡£ÈôÄúÒÔΪ±¾ÎÄÇÖÕ¼ÁËÄúµÄ°æȨÐÅÏ¢£¬»òÄú·¢Ã÷¸ÃÄÚÈÝÓÐÈκÎÉæ¼°ÓÐÎ¥¹«µÂ¡¢Ã°·¸Ö´·¨µÈÎ¥·¨ÐÅÏ¢£¬ÇëÄúÁ¬Ã¦ÁªÏµ×ðÁú¿­Ê±¹ÙÍøµÇ¼ʵʱÐÞÕý»òɾ³ý¡£

Ïà¹ØÐÂÎÅ

ÁªÏµ×ðÁú¿­Ê±¹ÙÍøµÇ¼

18523999891

¿É΢ÐÅÔÚÏß×Éѯ

ÊÂÇéʱ¼ä£ºÖÜÒ»ÖÁÖÜÎ壬9:30-18:30£¬½ÚãåÈÕÐÝÏ¢

QR code
ÍøÕ¾µØͼ