running crontab as different user

I have sudo crontab -e access... but all my cron jobs are  running as root user ... I want to run them under my user account and profile..

How can I do that?

basically I am looking for windows "run as" alternative in linux
LVL 5
CalmSoulAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ozoCommented:
su user
or put the job in the users crontab
0
CalmSoulAuthor Commented:
my company doesn't allow running crontab as user... they want all as root

so

su -u user command

like this?
0
CalmSoulAuthor Commented:
Also issue is - I want to run crontab under that user's profile...
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

woolmilkporcCommented:
Just

crontab -e

(without"sudo").
0
CalmSoulAuthor Commented:
@woolmilkporc Account doesn't have permission to do crontab -e... I can only run sudo crontab -e
0
woolmilkporcCommented:
So you don't have may options left, I fear.

Please run "sudo -l" to check what you're allowed to do.

If permitted, you could edit your user crontab by hand, but I'm afraid that you're not allowed to perform the maybe required cron daemon restart (depends on the cron implementation used).

Which exactly is your OS?

You could write your own litlle scheduler and run it as a background process, but your scheduling requirements shouldn't be too complicated then.

For one time jobs to be run some time in the future you could use "at" - if permitted, that is.
0
omarfaridCommented:
If you can edit the root crontab jobs, then you can use su username -c /path/to/command to run jobs under different users
0
CalmSoulAuthor Commented:
omarfarid:

 su username -c /path/to/command  <-- this will run under that username's bash profile ?
0
woolmilkporcCommented:
You must add a "-" to "su" in order to get the user's full environment:

* * * * * su - username -c "/path/to/command"  ## this will run under that username's bash profile!
0
omarfaridCommented:
You need to know that if /path/to/command is a shell script then you can always specify the shell to use to run the script with, put below line at the beginning of your script:

#!/bin/bash
0
Mohamed MagdyCTOCommented:
crontab -u USERNAMR -e /path/to/script

Change USERNAME to the desired one.
0
CalmSoulAuthor Commented:
I am getting confuse two people giving different answers - please clarify:

which of these is correct?


crontab -u USERNAMR -e /path/to/script

* * * * * su - username -c "/path/to/command"  ## this will run under that username's bash profile!
0
woolmilkporcCommented:
You said the only command you're allowed to run is "sudo crontab -e"!

So why should you be able to issue a command like the first one ("crontab -u ... ...") above? It will not work.

If you're allowed to run "sudo crontab" with other flags than just "-e" and if your "crontab" implementation supports "-u" you can try something like the first command above prefixed with "sudo":

sudo crontab -u your_username -e

"/path/to/script" is wrong anyway if used with -e. If used without -e the specified file will replace the respective crontab entirely.

Further, your_username must not be listed in /etc/cron.deny, or must be listed in /etc/cron.allow, depending on the particular setup used. If neither of these files exists, only root will be allowed to use "crontab" and run cron jobs.

The second thing above is an entry in root's crontab to be added with "sudo crontab -e". This will work.
0
simon3270Commented:
(at the risk of adding a third voice to this..) use the su - USERNAME -c "/path/to.script" one.

You aren't allowed personal crontabs even if it were technically possible to create on, so none of the "crontab -u username" options are available.

Use the above "su" command, in root's crontab (the only one you can use) to get root to run the command as another user, in this case USERNAME.

One thing to be wary of - some cron systems have a *very* short maximum command length.  Once you have used the "su - USERNAME-c" part, you may not have many characters left - keep your paths and script names short!
0
arnoldCommented:
Many options have been covered:omarfarid and woolmilkproc provided with the solution available to you

All entries must be within the roots cron tab (sudo crontab -e)
The line
* * * * * su - <your username> -c /path/to/command_script
You've not provided the schedule and both included the one that will run every minute.
0
CalmSoulAuthor Commented:
Cron command: * * * * * su - <your username> -c /path/to/command_script didn't work for me


My command was like:

su - username -c <command>
0
Mohamed MagdyCTOCommented:
Did you try my answer?
0
CalmSoulAuthor Commented:
modserv:

I am going to try that tonight .. thanks
0
woolmilkporcCommented:
The first five asterisks are the entries for the time schedule: minute, hour etc.

There is no crontab entry without those five fields on the right!

What you should have done is:

- Issue "sudo crontab -e"

- In the now opened text file add the full line, where the first five fields indicate your time schedule, the sixth field is the one starting with "su - ..." up to the end of the line.

Run "man crontab" to learn which values to use for specifying a time schedule.

- save the file with :wq! or :x
0
arnoldCommented:
What did not work?  Your issue might be that the username does not have rights to perform the tasks.
Using /bin/su just in case su can not be found based on PATH set or crons.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Linux

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.