Link to home
Start Free TrialLog in
Avatar of Kyle Hamilton
Kyle HamiltonFlag for United States of America

asked on

execute scripts using different users through cron

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?
ASKER CERTIFIED SOLUTION
Avatar of Kyle Hamilton
Kyle Hamilton
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Kyle Hamilton

ASKER

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.
Avatar of simon3270
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!!