{"id":9393,"date":"2017-02-06T15:35:46","date_gmt":"2017-02-06T14:35:46","guid":{"rendered":"https:\/\/www.oderland.se\/support\/artikel\/hur-skapar-jag-cronjobb\/"},"modified":"2026-05-18T11:39:00","modified_gmt":"2026-05-18T10:39:00","slug":"how-do-i-set-up-cron-jobs","status":"publish","type":"ht_kb","link":"https:\/\/www.oderland.se\/support\/en\/artikel\/how-do-i-set-up-cron-jobs\/","title":{"rendered":"How do I set up cron jobs?"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">Getting started with cron<\/h2>\n\n\n\n<p>Via <a rel=\"noreferrer noopener\" aria-label=\"cPanel (\u00f6ppnas i en ny flik)\" href=\"https:\/\/www.oderland.se\/support\/en\/artikel\/how-do-i-login-to-cpanel\/\" target=\"_blank\">cPanel<\/a>, you can find the cron settings via the icon <code>Cron Jobs<\/code> under <code>Advanced<\/code>.<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full is-style-default\"><a href=\"https:\/\/www.oderland.se\/support\/wp-content\/uploads\/2020\/01\/cpanel_icon_cron_jobs.png\"><img loading=\"lazy\" decoding=\"async\" width=\"700\" height=\"311\" src=\"https:\/\/www.oderland.se\/support\/wp-content\/uploads\/2020\/01\/cpanel_icon_cron_jobs.png\" alt=\"\" class=\"wp-image-18735\" srcset=\"https:\/\/www.oderland.se\/support\/wp-content\/uploads\/2020\/01\/cpanel_icon_cron_jobs.png 700w, https:\/\/www.oderland.se\/support\/wp-content\/uploads\/2020\/01\/cpanel_icon_cron_jobs-300x133.png 300w, https:\/\/www.oderland.se\/support\/wp-content\/uploads\/2020\/01\/cpanel_icon_cron_jobs-50x22.png 50w, https:\/\/www.oderland.se\/support\/wp-content\/uploads\/2020\/01\/cpanel_icon_cron_jobs-60x27.png 60w, https:\/\/www.oderland.se\/support\/wp-content\/uploads\/2020\/01\/cpanel_icon_cron_jobs-100x44.png 100w\" sizes=\"auto, (max-width: 700px) 100vw, 700px\" \/><\/a><\/figure>\n\n\n\n<p>Choose when the cron job should be run and enter the <code>Command<\/code> you wish to run, then click <code>Add New Cron Job<\/code>.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large is-style-default\"><a href=\"https:\/\/www.oderland.se\/support\/wp-content\/uploads\/2020\/02\/cpanel_cron_time_settings_en.png\"><img loading=\"lazy\" decoding=\"async\" width=\"706\" height=\"601\" src=\"https:\/\/www.oderland.se\/support\/wp-content\/uploads\/2020\/02\/cpanel_cron_time_settings_en.png\" alt=\"\" class=\"wp-image-9401\"\/><\/a><\/figure>\n\n\n\n<p>You have to enter the command in the following fashion:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted toolbar-overlay:false lang:sh decode:true\">\/path\/to\/program \/home\/yourusername\/path_to_script &gt; \/dev\/null 2&gt;&amp;1<\/pre>\n\n\n\n<p>For example, if you want to run a php script, the command could look like this:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted toolbar-overlay:false lang:sh decode:true\">\/usr\/local\/bin\/php \/home\/yourusername\/script.php > \/dev\/null 2>&amp;1<\/pre>\n\n\n\n<p class=\"wp-block-ht-blocks-messages wp-block-hb-message wp-block-hb-message--withicon is-style-info\">If you want to run a specific PHP version in the cronjob, enter <code>\/opt\/alt\/php73\/usr\/bin\/php<\/code> instead of <code>\/usr\/local\/bin\/php<\/code>.<br><br>You may <a href=\"https:\/\/www.oderland.se\/support\/en\/artikel\/how-do-i-manage-php\/#which-php-versions-are-available\" target=\"_blank\" rel=\"noreferrer noopener\">choose any installed PHP version<\/a> by swapping out 73 in the command above for any other PHP version (<em>e.g.<\/em> 70 if you want to use PHP 7.0).<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Using a URL in a cronjob<\/h2>\n\n\n\n<p>If your website is running a system that requires cronjobs that are run via URLs, you cannot simply enter the URL as the cron command to run. Instead, you need to tell the system to use the command <code>wget<\/code>. This will tell the server to visit the entered URL, in the same way as if you were to visit it via your web browser. Hence, enter the command for your cronjob as follows:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted toolbar-overlay:false lang:sh decode:true\">wget -O \/dev\/null -o \/dev\/null \"https:\/\/www.exempel.se\/command\"<\/pre>\n\n\n\n<p>Swap out <code>https:\/\/www.exempel.se\/command<\/code> for the proper URL needed to run your cronjob. Make sure that you keep the quotation marks around the URL, otherwise it may not work.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Managing output<\/h2>\n\n\n\n<p>If you wish, you can choose that an email should be sent to you after each cron job run. This can be practical if you want to see whether or not the job completed successfully. It may also help you troubleshoot a cron job that is not functioning as expected &#8211; the mail may then contain helpful error messages. If you want to receive those emails, you have to remove &#8220;&gt; \/dev\/null 2&gt;&amp;1&#8221; from the cron job command:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted toolbar-overlay:false lang:sh decode:true\">\/usr\/local\/bin\/php \/home\/yourusername\/path_to_script.php<\/pre>\n\n\n\n<p>You can select the receiving email address in the <code>Email<\/code> field just above where you set the time for your cron job.<\/p>\n\n\n\n<p><strong>Please note!<\/strong> If you want to run <code>wget<\/code> via cron and don&#8217;t wish any output, you must set both the -o and -O flags to \/dev\/null, for example:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted toolbar-overlay:false lang:sh decode:true\">wget -O \/dev\/null -o \/dev\/null \"https:\/\/www.exempel.se\"<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Overlapping cron jobs<\/h2>\n\n\n\n<p>A problem that may arise when running cron jobs is that you don&#8217;t know exactly how long each run of the command will take. If you have a command that runs every five minutes and finishes in three, all is fine; but if you have a command that runs every five minutes and requires seven minutes to finish you run into trouble. In practicality, this will result in multiple cron jobs running at once &#8211; something you want to avoid.<\/p>\n\n\n\n<p>There is, however, a solution: Flock. Flock creates a lock file when the cron job starts, and if the file still exists the next time the cron job is supposed to start, it won&#8217;t. When the cron job that created the lock file is done, the file is removed automatically.<\/p>\n\n\n\n<p>You can use Flock in the following way:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">\/usr\/bin\/flock -w 1 \/tmp\/cron.lock \/usr\/local\/bin\/php -q \/home\/yourusername\/path_to_script.php<\/pre>\n\n\n\n<p><code>\/usr\/bin\/flock<\/code> is the path to the Flock binary, and <code>\/tmp\/cron.lock<\/code> the path to the lock file &#8211; you do not have to edit those. You enter the rest of the cron job command after the lock file path, as described above.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Getting started with cron Via cPanel, you can find the cron settings via the icon Cron Jobs under Advanced. Choose when the cron job should be run and enter the Command you wish to run, then click Add New Cron Job. You have to enter the command in the following&#8230;<\/p>\n","protected":false},"author":12,"comment_status":"open","ping_status":"closed","template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"ht-kb-category":[214],"ht-kb-tag":[],"class_list":["post-9393","ht_kb","type-ht_kb","status-publish","format-standard","hentry","ht_kb_category-cpanel-whm"],"acf":[],"_links":{"self":[{"href":"https:\/\/www.oderland.se\/support\/en\/wp-json\/wp\/v2\/ht-kb\/9393","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.oderland.se\/support\/en\/wp-json\/wp\/v2\/ht-kb"}],"about":[{"href":"https:\/\/www.oderland.se\/support\/en\/wp-json\/wp\/v2\/types\/ht_kb"}],"author":[{"embeddable":true,"href":"https:\/\/www.oderland.se\/support\/en\/wp-json\/wp\/v2\/users\/12"}],"replies":[{"embeddable":true,"href":"https:\/\/www.oderland.se\/support\/en\/wp-json\/wp\/v2\/comments?post=9393"}],"version-history":[{"count":21,"href":"https:\/\/www.oderland.se\/support\/en\/wp-json\/wp\/v2\/ht-kb\/9393\/revisions"}],"predecessor-version":[{"id":25238,"href":"https:\/\/www.oderland.se\/support\/en\/wp-json\/wp\/v2\/ht-kb\/9393\/revisions\/25238"}],"wp:attachment":[{"href":"https:\/\/www.oderland.se\/support\/en\/wp-json\/wp\/v2\/media?parent=9393"}],"wp:term":[{"taxonomy":"ht_kb_category","embeddable":true,"href":"https:\/\/www.oderland.se\/support\/en\/wp-json\/wp\/v2\/ht-kb-category?post=9393"},{"taxonomy":"ht_kb_tag","embeddable":true,"href":"https:\/\/www.oderland.se\/support\/en\/wp-json\/wp\/v2\/ht-kb-tag?post=9393"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}