execute scripts using different users through cron

Kyle Hamilton
Kyle Hamilton used Ask the Experts™
on
Hi,

I'm trying to execute several scripts through cron.
Some scripts require being run as different users.

so through cli, I can do:

sudo -u username . /path/to/script.sh

Open in new window


but inside a script that is triggered with cron, this doesn't work.

So I tried it like this:

su --shell=/bin/bash --session-command="/path/to/script.sh" username &

Open in new window


without success.

Any ideas?
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Data Scientist
Most Valuable Expert 2014
Commented:
ha, I was missing the bash part.

so for posterity, this works:

su --shell=/bin/bash --session-command=". /path/to/script.sh" username &

Open in new window

Kyle HamiltonData Scientist
Most Valuable Expert 2014

Author

Commented:
Accepting my own answer, but only because I figured it out before anyone had a chance to help. I'd like to leave this for anyone else that might have a similar issue.
Alternatively, make /path/to/script.sh executable.

Also, you don't need the & at the end - cron runs jobs separately from each other, and expects them to run in the foreground.

Otherwise fine!!

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial