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?
Shell ScriptingLinux

Avatar of undefined
Last Comment
simon3270
ASKER CERTIFIED SOLUTION
Avatar of Kyle Hamilton
Kyle Hamilton
Flag of United States of America image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of Kyle Hamilton
Kyle Hamilton
Flag of United States of America image

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
simon3270
Flag of United Kingdom of Great Britain and Northern Ireland image

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!!
Linux
Linux

Linux is a UNIX-like open source operating system with hundreds of distinct distributions, including: Fedora, openSUSE, Ubuntu, Debian, Slackware, Gentoo, CentOS, and Arch Linux. Linux is generally associated with web and database servers, but has become popular in many niche industries and applications.

71K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo