Operating System - HP-UX
1839982 Members
2094 Online
110158 Solutions
New Discussion

Running cron jobs for a different userid

 
SOLVED
Go to solution
Ted Blahunka
Occasional Advisor

Running cron jobs for a different userid

I have a script that uses the mail utility
to extract a message to a file for a particular user.

I would like to run this from a cron job WITHOUT having to give the user cron.allow access.

The mail utility defaults to the user who
owns the cron process and I need it to check
the mail from a different user.

Does anyone know of a way to run a cron job
via the root cron, but somehow redirect/
re-address the userid that job runs against
so that it will check THAT user's mailbox
insteadof the root mailbox??

Thanks in advance

Ted Blahunka
tblahunka@enh.org
5 REPLIES 5
Patrick Wallek
Honored Contributor
Solution

Re: Running cron jobs for a different userid

You should be able to do something like:

su - username -c script_to_run

Put this in roots crontab and put in the appropriate username and it should work.
Ted Blahunka
Occasional Advisor

Re: Running cron jobs for a different userid

Patrick

That worked great.

Thanks for the tip
Thierry Poels_1
Honored Contributor

Re: Running cron jobs for a different userid

hmmm, I think the mail-part will fail. Since the cronjob is executed by root (with or without su - username xxx) root will receive all standard output and error in its mailbox.
(not sure for the full 100% but very close)
Better would be the redirect all output in your script and mail it if required.
regards,
Thierry.
All unix flavours are exactly the same . . . . . . . . . . for end users anyway.
Ted Blahunka
Occasional Advisor

Re: Running cron jobs for a different userid

Thierry

I tried Pat's suggestion and it does work
the way I wanted it to. To test , I put a
sleep statement in the script so I could
monitor the cron invoked process, and it
does show up running under . The
only glitch was that the output file was
still owned by root, but a chown in the
script after the redirect finishes fixed
that as well.

Thanks for your input

Ted
Thierry Poels_1
Honored Contributor

Re: Running cron jobs for a different userid

hi again,
I fully agree Patrick's idea is THE way to have a cron job being executed without explicitely giving crontab permission to a user.

But as I presumed (and have just verified :) if the cronjob has any standard output or error then this will be mailed to root, and not to the su'ed user! You will explicitely have to redirect all output in the cronjobs yourself.

regards,
Thierry.
All unix flavours are exactly the same . . . . . . . . . . for end users anyway.