{"id":198,"date":"2024-01-18T02:40:31","date_gmt":"2024-01-18T10:40:31","guid":{"rendered":"https:\/\/blog.sacko.dev\/?p=198"},"modified":"2024-01-25T17:34:11","modified_gmt":"2024-01-26T01:34:11","slug":"how-to-run-scripts-via-crontab","status":"publish","type":"post","link":"https:\/\/blog.sacko.dev\/?p=198","title":{"rendered":"How to Run Automated Scripts with Crontab"},"content":{"rendered":"\n<p>Learning crontab could be useful for any system maintenance or repetitive tasks you may need to do, such as file backups or system updates. As an example, I am using it as a way to do a &#8216;git clone&#8217; a few times a day to keep a website&#8217;s contents up-to-date, as I have the website&#8217;s owner using Github for the website&#8217;s CMS.<\/p>\n\n\n\n<p>First, you will want to make a script for the crontab. Use touch to create the script file, chmod +x to make it executable, and visudo to open up the sudoers file.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>touch <strong>\/path\/to\/script.sh<\/strong>\nchmod +x <strong>\/path\/to\/script.sh<\/strong>\nsudo visudo<\/code><\/pre>\n\n\n\n<p>You may not have to do this, but sometimes your script may need to be run as sudo to do some of the commands. If your commands will need elevated privileges to run, add this line to the sudoers file:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>linux_user<\/strong> ALL=(ALL:ALL) NO PASSWD:<strong>\/path\/to\/script.sh<\/strong><\/code><\/pre>\n\n\n\n<p>Now you can create the script. Here&#8217;s an example one that just prints out Hello!:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#!\/bin\/sh\necho \"Hello!\"<\/code><\/pre>\n\n\n\n<p>Once you have saved that file, you can start on the cron job. Start the crontab editor:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>crontab -e<\/code><\/pre>\n\n\n\n<p>From here, go to the bottom of the page, and add a line to schedule a cron job. This will run the script once a minute, and log the output through journalctl, tagged with script-tag.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>* * * * * <strong>\/path\/to\/script.sh<\/strong> 2&gt;&amp;1 | logger -t <strong>script-tag<\/strong><\/code><\/pre>\n\n\n\n<p>Now, you should be able to see the logs with the journalctl command. This will give you a live feed of updates from your script:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>journalctl --follow -t <strong>script-tag<\/strong><\/code><\/pre>\n\n\n\n<p>That&#8217;s all there is to it for the basics. If you want to set up different times for the cron job to run, the syntax is as follows:<\/p>\n\n\n\n<p>mm hh dm MM dw command-to-execute<br>mm &#8211; minute (0-59)<br>hh &#8211; hour (0-23)<br>dm &#8211; Day of month (1-31)<br>MM &#8211; Month of year (1-12)<br>dw &#8211; Day of week (0-6 for Sunday to Saturday)<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Learning crontab could be useful for any system maintenance or repetitive tasks you may need to do, such as file backups or system updates. As an example, I am using it as a way to do a &#8216;git clone&#8217; a few times a day to keep a website&#8217;s contents up-to-date, as I have the website&#8217;s <a href=\"https:\/\/blog.sacko.dev\/?p=198\" class=\"more-link\">&#8230;<span class=\"screen-reader-text\">  How to Run Automated Scripts with Crontab<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"_links":{"self":[{"href":"https:\/\/blog.sacko.dev\/index.php?rest_route=\/wp\/v2\/posts\/198"}],"collection":[{"href":"https:\/\/blog.sacko.dev\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.sacko.dev\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.sacko.dev\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.sacko.dev\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=198"}],"version-history":[{"count":6,"href":"https:\/\/blog.sacko.dev\/index.php?rest_route=\/wp\/v2\/posts\/198\/revisions"}],"predecessor-version":[{"id":211,"href":"https:\/\/blog.sacko.dev\/index.php?rest_route=\/wp\/v2\/posts\/198\/revisions\/211"}],"wp:attachment":[{"href":"https:\/\/blog.sacko.dev\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=198"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.sacko.dev\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=198"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.sacko.dev\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=198"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}