0

A newly created crontab job is failing because the program can't read a .json file containing credentials in the same directory. Both files are contained in a /home/deploy/app/ folder. Crontab executes /home/deploy/app/app which needs to read home/deploy/app/config.json to execute. The program runs fine a the deploy user, but fails when run by crontab.

I am sure this is the reason the program is failing since I directed the crontab job to log its output, and I can see that the program is unable to read the config.json file. The app has the following permissions -rwxr-xr-x 1 deploy deploy and the config.json has -rw-rw-r-- 1 deploy deploy.

The crontab entry is:

10 0,6,9,12,15,18 * * * /home/deploy/gocode/bin/BuoyBot > /home/deploy/buoybot.log 2>&1

The cron log records the error from the app, which indicates is it unable to load the config.json file stored in the same folder as the app.

error loading config.json: invalid argument

This error does not occur when the program is run manually from within the /home/deploy/gocode/bin/ folder.

UPDATE: The solution, as suggested below, was to reference the full path of the config.json file within the app (/home/user/app/config.json), rather than a reference to the local directory (config.json).

surfearth
  • 103
  • 4
  • 1
    Please show us the output from the log file. – user9517 Nov 15 '15 at 18:56
  • Added additional information above. – surfearth Nov 15 '15 at 20:18
  • Work your way through the duplicate above. It will almost certainly help you solve your problem. If i does not it will help you gather information that will help us help you. That information should be edited into your question. – user9517 Nov 15 '15 at 20:24
  • I carefully read the above mentioned link, but it does not address the issue. Cron is successfully executing the program, however when it runs the program is not capable of accessing a supporting file in the same directory. More precisely, the command `/home/deploy/gocode/bin/BuoyBot` runs, but the `BuoyBot` program is unable to access the credentials loaded in /home/deploy/gocode/bin/config.json`. – surfearth Nov 16 '15 at 00:00
  • 1
    The app is trying to access to config.json or /full/path/config.json? – Jose Raul Barreras Nov 16 '15 at 03:55
  • Jose Raul Barreras question solved the issue. The app was referencing the local path. Once I updated the code to include the full path, the file successfully ran in Cron. – surfearth Nov 17 '15 at 02:57
  • The dupe question now includes a bit on cwd. – womble Nov 29 '15 at 05:29

2 Answers2

2

Without seeing the code my guess is that you are not providing the full path to your data file when you attempt to open it.

user9517
  • 114,104
  • 20
  • 206
  • 289
  • 1
    UPDATE: The solution, as suggested by lain, was to reference the full path of the config.json file within the app (/home/user/app/config.json), rather than a reference to the local directory (config.json). – surfearth Nov 20 '15 at 16:34
-1

Can you try launching the cron as the user deploy as follows:

10 0,6,9,12,15,18 * * * deploy /home/deploy/gocode/bin/BuoyBot > /home/deploy/buoybot.log 2>&1

Alternatively you can try as follows also:

10 0,6,9,12,15,18 * * * deploy cd /home/deploy/gocode/bin/ &&  ./BuoyBot > /home/deploy/buoybot.log 2>&1

Hope it helps.

  • Those are not valid user crontab entries, which is the kind of crontab the OP has indicated (s)he's using. – MadHatter Nov 16 '15 at 08:06
  • This would only work in the /etc/crontab (or similar) file which requires a user name. KIt may not be accessible to the OP. It doesn't work in a user crontab. – user9517 Nov 16 '15 at 08:07