How to run a script as different user inside cronjob in solaris.

Hi ,

I have a shell script to perform some actions on sun solaris box . This script normally requires to be run as a different user. so, whenever i have to run this script, i need to sudo in as that user , enter the password and execute it. Now,I have to setup a cronjob to execute the script every 15 mins .I have provded 777 permissions for that script .

But, that is not working inside the cronjob. I think the sudo is creating problems. Iam planning to create another script to call the original script .Is there a way to provide the password and the user id that i need to sudo in to run the script while running from a cronjob? Please advise how to handle this.

Thanks &Regards,
csg_user

Sudo gives you the option to allow a user to run commands without a password (ie. not requiring confirmation).

The alternative is to put the actions in root's crontab and use "su - user -c command".

*Never* do this, especially when using cron. It allows anyone to alter the script to perform tasks as that user.

or put the script in that user's crontab.

Hi Prowla,
Thanks for your reply .

My cron will have :
0,15,30,45 * * * * /opt/myfolder/myscriptname. I will use the same under root's cron .But,Where should i use the "su - user -c command". Please let me know.

Hi Porter,

Thanks. I tried running the script under that user's cron . But, it is not working . Am i missing something here?

Thanks.

I don't know, I don't know what your script does or how you've set it up, but 99% of problems with crontab are due to environment variables not being initialised.

Hi Thanks.

Will running the script under root resolve the problem?

Regards,
csg_user.

I would try and avoid stuff as root.

Can you describe step by step what your script does?

The script(script1) will recycle our application services. This script could only be executed as the application user . So, We run this script manually by sudo su - appl_user , then password , then run the script . $myscript name. Now , iam trying to automate it . So, iam calling in a cronjob .But, that is not working .

Actually, based on this result, i need to create another script(script2) which will call the script1 only if some application conditions are satisfied.
That is where the sudo/password for the appl_user is creating problems.

Please help .Many thanks in advance.

Confirm that script works from home directory of user you want it to run as when run from a terminal/ssh/telnet login.

That will prove if the user has all the rights etc.

Then do a "set" to print all the environment variables that are present before the script was run. Look through them and identify ones your script will need.

When the cron task runs assume none of those variables will be set (except a few, eg HOME).

As I said, 99% of cron problems are environment based.

This is the sort of thing that can be run rom root's cron:

0,15,30,45 * * * * su - myuser -c /opt/myfolder/myscriptname