Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

How could I run a script as a specific user (non-root)? As this?

sudo su - john /usr/share/script.pl for user john?

share|improve this question
4  
sudo -u john /usr/share/script.pl –  Miguel de Val-Borro Aug 8 '13 at 21:19
    
@MigueldeVal-Borro:But sudo su - john makes me john.So why it does not work for the script execution? –  Cratylus Aug 8 '13 at 21:21
1  
su is used to change to another user and sudo executes a command as a different user. They are different commands –  Miguel de Val-Borro Aug 8 '13 at 21:23
add comment

3 Answers

You can pass -c to su to execute a command. If sudo allows you to run the command, this should work:

sudo su - john -c "/usr/share/script.pl"

Keep in mind that by passing - to su your environment will be the same as if you had logged in, so if the script depends on environment variables (like current path) you may have to make adjustments.

share|improve this answer
add comment

use the -c option like

sudo su - john -c "/usr/share/script.pl"

Or rather than defining the user like above you can define a cron job for the specific user using the following command

crontab -u john -e

then set the cron, it will add cron job for the specific user better then defining the user in a root cron

share|improve this answer
add comment

Other answers give better answers to your specific question, here's some helpful explanation.

su = switch user

sudo = switch user and do X (with no extra options, the 'user' is 'root')

For most users su is not allowed without using sudo, which is meant to make the user think twice about what they're doing and usually to prompt again for password.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.