Operating System - HP-UX
1834835 Members
2279 Online
110070 Solutions
New Discussion

want to use su within cron to run as another user

 
SOLVED
Go to solution
Gregory Locke
Frequent Advisor

want to use su within cron to run as another user

I have one job that needs to run in cron as another user. I don't want to create another crontab for this one shot program.

The generic user login is notrig (home/notrig)

My syntax for crontab is:
30 01 01 * * /usr/bin/su - notrig /home/notrig/program >/tmp/notrig.program.out 2>&1

Any suggestions?
6 REPLIES 6
Patrick Wallek
Honored Contributor
Solution

Re: want to use su within cron to run as another user

You need to add a '-c' to your command line.

30 01 01 * * /usr/bin/su - notrig -c /home/notrig/program >/tmp/notrig.program.out 2>&1


For more details see the su man page.
James R. Ferguson
Acclaimed Contributor

Re: want to use su within cron to run as another user

Hi Gregory:

Since you are going to cause your '.profile' to be sourced (read), you should modify your '.profile' to only conditionally execute its terminal-handling logic --- the 'stty' and 'tset' commands --- when the process is associated with a terminal. This will eliminate the annoying "not a typewriter" output you will otherwise get.

To test if your process is associated with a terminal, you can do (for the purposes here):

if [ -t 0 ]; then
...
fi

...When the above is true, the process is associated with a terminal and it is "safe" to execute 'stty' and 'tset' commands.

Regards!

...JRF...
Gregory Locke
Frequent Advisor

Re: want to use su within cron to run as another user

Patrick:

Where did you find that -c option?

I didn't see it in any of the man pages.
Ivan Krastev
Honored Contributor

Re: want to use su within cron to run as another user

From man page - http://docs.hp.com/en/B2355-60103/su.1.html

"Execute the command, 'echo hello', using the temporary environment and permissions of user bin. In this example, user bin's shell is invoked with the arguments -c 'echo hello'.

su bin -c 'echo hello'
"

-c is argument to shell, not to su.


regards,
ivan
Gregory Locke
Frequent Advisor

Re: want to use su within cron to run as another user

Thanks Patrick, James and & Ivan for your help.
Gregory Locke
Frequent Advisor

Re: want to use su within cron to run as another user

James:

Where in a .profile would I place

if [ -t 0 ]; then
...
fi

to keep from getting errors like
more infx.ccnotice.out
ttytype: couldn't open /dev/tty for reading
stty: : Not a typewriter
Not a terminal
stty: : Not a typewriter
stty: : Not a typewriter
^[2 ^[2 ^[2 ^[2 ^[2 ^[2 ^[2 ^[2
^[2 ^[2 ^[2 ^[2 ^[2 ^[2 ^[2 ^[2 ^[2 ^[2
^[2 ^[2
stty: : Not a typewriter