1

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

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

  • 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
1

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.

0

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.

0

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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