Cron Job For .Sh

Hi,
I have a question. Have two .sh scripts on my node:

pg_backup_rotated.sh
tokensRefresh24h.sh

Both are in:

/var/lib/postgresql

and both have a+x privileges.
I put both of them in cron as a user postgres with crontab -e:

0 4 * * * ~/pg_backup_rotated.sh
0 2 * * * ~/tokensRefresh24h.sh

After I save the file, I use service cron reload command. Backup script is running fine at 4am everyday, but token refresh is not running at 2am … or at least something else is not wright - I see no action on the REST API on my server, tokens are not getting refreshed. If I run the script with ./tokensRefresh24h.sh as user postgres everything works - I get:

{"method":"refreshTokens","params":{},"result":"OK"}

from console. Do you have any idea what could I be doing wrong?

BR,
Wojtek

2 Replies

Try add sh before the script.

sh ~/tokensRefresh24h.sh

@wojtek --

You write:

If I run the script with ./tokensRefresh24h.sh as user postgres everything works - I get:
 
{"method":"refreshTokens","params":{},"result":"OK"}
 
from console. Do you have any idea what could I be doing wrong?

See: https://help.dreamhost.com/hc/en-us/articles/215767107-Execution-environment-of-a-cron-job

Don't use shell constructs like ~/ as a shortcut for $HOME in crontab(1) files. If it's understood at all, cron(8) will interpret ~/ not as the cron(8) user's $HOME but as the $HOME of the super-user (/root most likely…root is cron(8)'s owner). This is definitely not what you want.

cron(8) runs your script using the "default" environment (set up in a bunch of files in /etc that I can't remember the names of). My guess is that your script is failing because it's made the assumption that it's running as if it would run under user postgres's login shell. That's not the case. I would suggest that your other script is probably also failing for the same reason. Any evidence of its correct function is probably serendipitous.

@Tntdruid --

Your suggestion won't fix @wojtek's problem for the same reasons.

-- sw

Reply

Please enter an answer
Tips:

You can mention users to notify them: @username

You can use Markdown to format your question. For more examples see the Markdown Cheatsheet.

> I’m a blockquote.

I’m a blockquote.

[I'm a link] (https://www.google.com)

I'm a link

**I am bold** I am bold

*I am italicized* I am italicized

Community Code of Conduct