Search code examples
shellscriptingcron

Running bash script as cron job does'nt do anything


I have a Bash script at /home/pi/dev/app/script.sh

Which starts a node app to write "hello world" to a file. Manually starting the script using works fine. node app:

const fs = require('fs');

let test = 'cron working';
fs.writeFile('temp.txt', test, (err) => {

    if (err) throw err;


    console.log('saved!');
});

Bash-script:

#!/bin/bash
node /home/pi/dev/app/index.js

But:

running crontab -e

adding line: 1 * * * * /bin/bash /home/pi/dev/app/script.sh

Doesnt do anything. watching tail -f /var/log/syslog shows that the cron itself is being executed but it doesn't write into the file.

I tried sudo nano /etc/crontab adding 1 * * * * root /bin/bash /home/pi/dev/app/script.sh But this also doesnt work.

I am using a RPI4 running debian.


Solution

  • If the bash script line that launchs node is simply "node index.js", it's probably a missing PATH entry for the crond user environment. If crond is running as root, check the default environment for the root superuser, its PATH entry has to include the node.js path. You may have also to restart the running crond or kill -SIGHUP <crond pid> to refresh its environment settings.

    For security clearance, take care that the node.js path is only writable for the root superuser. If it can't be done, it would be then more safe to use the full node.js path in your bash script without adding node.js path entry to root's PATH instead.