crontab bash tests

Karl Auer kauer at biplane.com.au
Sun Sep 24 23:56:49 UTC 2023


On Sun, 2023-09-24 at 18:44 +0200, lejeczek via ubuntu-users wrote:
> I have this
> 
> */1 * * * * { [[ $(/usr/bin/sudo -u postgres /usr/bin/psql
> [...etc...]

Not a direct answer to your question, but I would suggest putting this
in a script, with comments so future you knows what it's supposed to be
doing and why, then running the script from cron. Run it from root's
crontab and make sure it is readable, writable and executable only by
root.

Three things leap out as possible problems - no space after the closing
parenthesis, the sudo, and ownership of the temp file. sudo will only
work reliably if this is in root's crontab, otherwise it will ask for a
password, and if the commands are not running as root, touch and rm may
fail. The outermost braces don't look necessary to me, but I may be
wrong there.

In general, you don't have to wrap a command or pipeline up in "$()" if
you just want to know if it succeeded:

   if pipeline ; then { do_something ; } fi

Or in your case

   psql && touch || rm

I would expect there to be output from your command if it were failing
- maybe in /var/log/syslog? Or emailed to you, so check your (and
root's) mail spools.

Good luck, let us know when you've figured it out :-)

Regards, K.


-- 
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Karl Auer (kauer at biplane.com.au)
http://www.biplane.com.au/kauer






More information about the ubuntu-users mailing list