cronpm2rscript

pm2 should (re-)start script once a week but does so every 5-6 minutes | cron-restart, ecosystem.config


On our server we run applications and scripts using pm2. From the econsystem.config.js file, I wanted to let run an R script once a week, Friday night at 00:00h. This is what my econsystem.config.js file looks like:

module.exports = {
    apps : [{
        name: "my-R-sctipt",
        script: "../my-R-script.R",
        instances: 1,
        exec_mode: "fork",
        out_file: "/dev/null",
        error_file: "/dev/null",
        interpreter: "Rscript",
        cron_restart: "0 0 * * 5",
    }]
};

The string "0 0 * * 5" is supposed to indicate that it should restart the app at 00:00 at the 5th day of the week, Friday.

Where am I going wrong?

my-R-sctipt is constantly running - as it needs to for cron_restart to work - and delivers the expected output, so the interpreter and the path are correctly specified.


Solution

  • I am not a specialist of R and don't know what is exactly doing your R script, but why do you need pm2 for that?

    Won't you just need to use the system cron?

    pm2 is perfect for running an application that is running forever, like for example an http server.

    In this case, cron_restart is used to restart the http server an a regular basis.

    if your R script does not loop, and you just need to execute it once a week, you should prefer schedule it with crontab rather than with pm2:

    # edit your crontab
    $ crontab -e
    

    and then add:

    0 0 * * 5 /path/to/my/R/script