How to run a command as a specific user in an init script?

后端 未结 6 2056
南方客
南方客 2020-12-29 03:26

I\'m writing an init script which is supposed to execute a single command as a user different than root. This is how I\'m doing it currently:
sudo -u username comm

6条回答
  •  自闭症患者
    2020-12-29 03:42

    Adding this answer as I had to lookup multiple places to achieve my use case. I had a script that runs on startup. This script runs process as a specific (passwordless) user and is running on multiple linux flavors. Here are options on different flavors: (I have taken java as target process for example)

    1. RHEL / CentOS 6:

    source /etc/rc.d/init.d/functions
    daemon --user=myUser $JAVA_HOME/bin/java
    

    2. RHEL 7 / SUSE12 / other linux flavors where systemd is used:

    In your systemd unit file add:

    User=myUser
    

    3. Suse 11:

    /sbin/startproc -u myUser $JAVA_HOME/bin/java

提交回复
热议问题