linux – bash script is started manually, but it will not start if you set it as a cron task

Question:

I have a bash script that makes a request to Postgresql and saves the result (overwriting) to a file after processing.

Simplified:

#!/bin/bash

TEXTDATA="\copy (SELECT json_agg(t) FROM (SELECT itemData FROM items ) as t) to /path/to/my/file.json"

#Мой запрос использующий TEXTDATA

sed -i 's/}, \\n {/}, {/g' /path/to/my/file.json

If I manually call the script in the terminal:

./myScript.sh

Then it works correctly and saves the result to a file.

However, if I write to crontab (by running sudo crontab -e ) an entry like this:

SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin

30 * * * * /bin/bash /home/myName/myScript.sh

Then the script does not run every 30 minutes. Tried it both with SHELL / PATH and / bin / bash, and without – does not work.

Please tell me what I am doing wrong and how can I fix it?

Answer:

As a result, for the script to work, I needed:

  1. Run crontab -e without sudo
  2. Remove PATH=/sbin:/bin:/usr/sbin:/usr/bin and /bin/bash , but leave SHELL=/bin/bash – it does not work without it.
  3. Set bash file permissions with sudo chmod 777 /path/to/myScript.sh
SHELL=/bin/bash

30 * * * * /home/myName/myScript.sh

Thank you all for your help.

Scroll to Top