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
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

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.

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


su -u user command

like this?
CalmSoulAuthor Commented:
Also issue is - I want to run crontab under that user's profile...
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.


crontab -e

CalmSoulAuthor Commented:
@woolmilkporc Account doesn't have permission to do crontab -e... I can only run sudo crontab -e
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.
If you can edit the root crontab jobs, then you can use su username -c /path/to/command to run jobs under different users
CalmSoulAuthor Commented:

 su username -c /path/to/command  <-- this will run under that username's bash profile ?
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!
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:

Mohamed MagdyCTOCommented:
crontab -u USERNAMR -e /path/to/script

Change USERNAME to the desired one.
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!
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.
(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!
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.
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>
Mohamed MagdyCTOCommented:
Did you try my answer?
CalmSoulAuthor Commented:

I am going to try that tonight .. thanks
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
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.

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

From novice to tech pro — start learning today.