{"id":324,"date":"2023-05-19T08:55:23","date_gmt":"2023-05-19T04:55:23","guid":{"rendered":"https:\/\/kidds.co.za\/?p=324"},"modified":"2023-05-19T08:58:39","modified_gmt":"2023-05-19T04:58:39","slug":"crontab","status":"publish","type":"post","link":"https:\/\/kidds.co.za\/index.php\/2023\/05\/19\/crontab\/","title":{"rendered":"Crontab"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\" id=\"cron-schedule-syntax\">Cron Schedule Syntax<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>Things to keep in mind to make your life easier in the long run.\n<\/strong>1. Try not to run cronjobs as a user that might be temporary, i.e. a server admin that might not be there in 2 years, people rotate jobs.\n2. Set the crons to run as a system user with the minimum required permission that the script needs, not root. Running a cronjob as root is stupid, unless absolutely required.\ncrontab -u &lt;user> -e \/ crontab -u www-data -e \nThis will run the cronjob with permissions of Apache on Ubuntu.\n\n3. DO NOT RUN AS ROOT. you will forget about it, at some point you or another admin will alter the script and it is running with permissions that can destroy things.\n\nCreate a file on every server in the same location that will not disappear if a user is removed. (\/etc\/THISSERVERSCRONJOBS.info) is fine. List all the cronjobs on the server, what they do, who wrote the scripts, what their primary purpose is, what will happen if they dont run on the set schedule.\n\nIf a server is a little loaded dont let a cron run every minute * * * * * is too common and easy. \nFor the minutes \n*\/2 will make it run every 2nd minute (50% reduction in server resource requirement for this script instantly) \n*\/5 every 5th minute, use this to balance a server where a cron is not needed every minute.\nBalance all your crons like this so you dont get stupid spikes for no reason as all scripts try run at the exact same time every minute.\n\nDO NOT SET CRONS UNDER YOUR PERSONAL USER if it is needed for server related tasks, even you might leave or another admin might see a problem or want to add a feature, they will not check you personal cronjobs.<\/code><\/pre>\n\n\n\n<p>A basic crontab entry looks something like this:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>*    *    *    *    *   \/home\/user\/bin\/somecommand.sh\n|    |    |    |    |    Command or Script to execute\n|    |    |    | Day of week(0-6 | Sun-Sat)\n|    |    |  Month(1-12)\n|    |  Day of Month(1-31)\n|   Hour(0-23)\nMin(0-59)<\/code><\/pre>\n\n\n\n<p><a href=\"https:\/\/crontab.guru\/\" target=\"_blank\" rel=\"noreferrer noopener\">https:\/\/crontab.guru\/<\/a> &#8211; this is just an awesome site to get the entry for a specific schedule.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"managing-crontab-entries\">Managing Crontab Entries<\/h2>\n\n\n\n<p>Once cron is running, it checks for crontab entries in the following files every minute:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\/etc\/crontab<\/strong><\/li>\n\n\n\n<li><strong>\/var\/spool\/cron\/crontabs\/$USER<\/strong>&nbsp;(where&nbsp;<code>$USER<\/code>&nbsp;is the currently logged-in user)<\/li>\n<\/ul>\n\n\n\n<p>The first file,&nbsp;<strong>\/etc\/crontab<\/strong>, is a system-generated file containing shortcut commands designed to check for cron table entries in the following directories:&nbsp;<code>\/etc\/cron.hourly<\/code>,&nbsp;<code>\/etc\/cron.daily<\/code>,&nbsp;<code>\/etc\/cron.weekly<\/code>, and&nbsp;<code>\/etc\/cron.monthly<\/code>.  <\/p>\n\n\n\n<p>For the purpose of this article, you will concentrate on creating new crontab entries for your user.<\/p>\n\n\n\n<p>You can achieve this by using the&nbsp;<code>crontab<\/code>&nbsp;command. From the terminal, enter&nbsp;<em>edit<\/em>&nbsp;mode for your user\u2019s crontab using the following command:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ crontab -e<\/code><\/pre>\n\n\n\n<p>The first time you run this command, the OS should ask you what editor you would like to use with a little menu like this:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>no crontab for user - using an empty one\n\nSelect an editor.  To change later, run 'select-editor'.\n  1. \/bin\/nano    \t&lt;---- easiest\n  2. \/usr\/bin\/vim.basic\n  3. \/usr\/bin\/vim.tiny\n  4. \/bin\/ed\n\nChoose 1-4 &#91;1]:<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Cron Schedule Syntax A basic crontab entry looks something like this: https:\/\/crontab.guru\/ &#8211; this is just an awesome site to get the entry for a specific schedule. Managing Crontab Entries Once cron is running, it checks for crontab entries in the following files every minute: The first file,&nbsp;\/etc\/crontab, is a system-generated file containing shortcut commands [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[26,2],"tags":[],"class_list":["post-324","post","type-post","status-publish","format-standard","hentry","category-crontab","category-linux"],"_links":{"self":[{"href":"https:\/\/kidds.co.za\/index.php\/wp-json\/wp\/v2\/posts\/324","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/kidds.co.za\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kidds.co.za\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kidds.co.za\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/kidds.co.za\/index.php\/wp-json\/wp\/v2\/comments?post=324"}],"version-history":[{"count":3,"href":"https:\/\/kidds.co.za\/index.php\/wp-json\/wp\/v2\/posts\/324\/revisions"}],"predecessor-version":[{"id":330,"href":"https:\/\/kidds.co.za\/index.php\/wp-json\/wp\/v2\/posts\/324\/revisions\/330"}],"wp:attachment":[{"href":"https:\/\/kidds.co.za\/index.php\/wp-json\/wp\/v2\/media?parent=324"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kidds.co.za\/index.php\/wp-json\/wp\/v2\/categories?post=324"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kidds.co.za\/index.php\/wp-json\/wp\/v2\/tags?post=324"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}