{"id":1760,"date":"2017-09-27T14:52:34","date_gmt":"2017-09-27T14:52:34","guid":{"rendered":"https:\/\/ibex.tech\/cloud\/?p=1760"},"modified":"2022-02-17T07:14:02","modified_gmt":"2022-02-17T07:14:02","slug":"cron-scheduled-tasks","status":"publish","type":"post","link":"https:\/\/ibex.tech\/cloud\/platforms\/aws\/elastic-beanstalk\/php-applications\/cron-scheduled-tasks","title":{"rendered":"cron scheduled tasks"},"content":{"rendered":"<h4>\nCreate CRON&nbsp;Task For Web Server Environment Example&nbsp;<br \/>\n<\/h4>\n<p>\nYes you can create cron&nbsp;tasks in the web server environment which can be handy when you want to do background things you&#39;d otherwise have to spin up a worker environment to do (at extra running cost).&nbsp; You can create them in worker environments too but there&#39;s a simpler way provided by aws to define them in worker environments&nbsp;if preferred (google it).&nbsp; The following has been tested on a web server environment only.\n<\/p>\n<p>\nIn the &quot;.ebextensions&quot; folder create a config file or add to your existing config file.&nbsp; In this example the config file is called &quot;project.config&quot; (the name doesn&#39;t actually matter)\n<\/p>\n<p>\nAdd the following to it:\n<\/p>\n<pre>\r\n<code>\r\ncontainer_commands:\r\n  01_remove_crontab:\r\n    command: &quot;crontab -r || exit 0&quot;\r\n  02_add_crontab:\r\n    command: &quot;cat .ebextensions\/my_cron_job.txt | crontab&quot;\r\n<\/code><\/pre>\n<p>\nNote you must uses spaces not tabs in this file\n<\/p>\n<p>\nNow create a file called &quot;my_cron_job.txt&quot; and add the following to it:\n<\/p>\n<pre>\r\n<code>\r\n# The newline at the end of this file is extremely important.  Cron won&#39;t run without it.\r\n# Run every 1 minute\r\n* * * * * \/usr\/bin\/php \/var\/app\/current\/my_php_file.php \r\n# End\r\n\r\n<\/code><\/pre>\n<p>\nImportant:\n<\/p>\n<p style=\"margin-left: 40px;\">\nThere must be a blank line at the end of the file.&nbsp; It may also be necessary to avoid blank lines earlier in the file (not checked).\n<\/p>\n<p style=\"margin-left: 40px;\">\nIf you are creating the file in windows add a space at the end of the cron&nbsp;definition (after the &quot;.php&quot; as windows will add a &lt;CR&gt; before the &lt;LF&gt; which linux doesn&#39;t like, but the space before it will let the line through.\n<\/p>\n<p style=\"margin-left: 40px;\">\nDon&#39;t add &quot;root&quot; after the time definition to specify root user, it stops it working (it will execute as root anyway &#8211; you can see this in the log once its running)\n<\/p>\n<p style=\"margin-left: 40px;\">\nIf your php file &quot;includes&quot; any other file bear in mind you may need to provide the full path to the file as the php file is being called by cron&nbsp;in a different working directory.&nbsp; In our case it wasn&#39;t necessary, but one to be aware of in case.&nbsp; It may be possible to solve it by using the following&nbsp;(not tried but saw it somewhere):&nbsp;* * * * * cd \/var\/app\/current\/; \/usr\/bin\/php \/var\/app\/current\/my_php_file.php\n<\/p>\n<h4>\nView cron&nbsp;log via SSH<br \/>\n<\/h4>\n<pre>\r\n<code>\r\nsudo grep CRON \/var\/log\/cron\r\n<\/code><\/pre>\n<p>\n&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Create CRON&nbsp;Task For Web Server Environment Example&nbsp; Yes you can create cron&nbsp;tasks in the web server environment which can be handy when you want to do background things you&#39;d otherwise have to spin up a worker environment to do (at extra running cost).&nbsp; You can create them in worker environments too but there&#39;s a simpler [&hellip;]<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[140],"tags":[],"class_list":["post-1760","post","type-post","status-publish","format-standard","hentry","category-php-applications"],"_links":{"self":[{"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/posts\/1760","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/comments?post=1760"}],"version-history":[{"count":6,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/posts\/1760\/revisions"}],"predecessor-version":[{"id":1766,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/posts\/1760\/revisions\/1766"}],"wp:attachment":[{"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/media?parent=1760"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/categories?post=1760"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/tags?post=1760"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}