Is it possible to have cron job running on Amazon lightsail instance?
Asked Answered
D

2

16

I would like to set up periodic jobs on an Amazon Lightsail instance, but I can find no information on it - only for Amazon EC2.

Is it possible to issue cron jobs on a lightsail instance, or do I need to change to EC2?

Deathblow answered 21/10, 2017 at 13:9 Comment(0)
D
25

To answer my own question, just 8 hours later: It IS possible to issue cron jobs on Amazon Lightsail instances

Here is a working example of running a PHP script:

  1. Connect to your Lightsail instance either by logging in to your lightsail account and clicking "Connect using SSH" or using a SSH client, such as PuTTY.
  2. Create a folder called 'projects' in /home/bitnami/ and create a simple .php file called Hello World:

    <?php print("Hello World"); ?>
    
  3. Use command crontab -e to access a document, from where you can add lines that will be your scheduled cron jobs.

  4. Append two lines to the document AND ADD A NEWLINE:

    PATH=/usr/bin:/bin:/opt/bitnami/php/bin:
    * * * * * php -f /home/bitnami/projects/HelloWorld.php > /home/bitnami/projects/Out.put
    

    Line 1 adds php to the cron path (Cron sees a different path than what is given in the environment variables. Type: env and hit enter in the console). To see which env variables are given to cron replace line 2 with: * * * * * env > /tmp/env.output and look in the file. Line 2 is the cron schedule. The asterisks means: Do this every minute of every hour, day, month, year. Search Google for this :) And will output to a file called Out.put.

  5. Wait 1 minute and see that Out.put has been created and contains the magic words of Hello World

If you are having problems with cron jobs not working, check out this troubleshooting guide: https://stackoverflow.com/tags/cron/info

Hope this helps. If it does not, post a comment before downvoting!

Deathblow answered 21/10, 2017 at 22:14 Comment(3)
Make sure you are using absolute paths if you are using include or require in your PHP file. This post explains CRON and relative paths: #1969874 I couldn't get this answer to work and it was not the CRON setup, but my PHP file that was the problem.Sontich
how would this look on servers that do not run bitname but plain PHP? Also, what if the cronjob should open a site on another server? Can this be done?Cahra
Try running which wp and which php from the command line to see the path of your executables.Tricuspid
E
2

If you are using node.js, you can do this by adding a few lines to your crontab. First, open the crontab

crontab -e

Add these three lines at the end of the file. I'm assuming you are using forever.

PATH=/usr/bin:/bin:/opt/bitnami/nodejs/bin:
@reboot cd /home/bitnami/**THISISYOURFOLDER** && /opt/bitnami/nodejs/bin/forever **NAMEOFYOURAPP.JS**
0 1 * * * sudo reboot

This will run your app that is in THISISYOURFOLDER when the system reboots. In addition, I'm setting it up to reboot at 1am everyday with this setting.

To test this, you can use

* * * * * cd /home/bitnami/**THISISYOURFOLDER** && touch myfile.txt

in your crontab. This needs to create myfile.txt in your target folder after a minute. If it doesn't you need to change your path setting. There is also a difference between

sudo crontab -e
crontab -e

Find time settings at https://crontab.guru/

Embody answered 31/1, 2019 at 0:24 Comment(1)
Wouldn't it be easier to use a node package for cron if you're using node?Frosting

© 2022 - 2024 — McMap. All rights reserved.