Automic Workload Automation

Expand all | Collapse all

Switch to other User and execute commands

  • 1.  Switch to other User and execute commands

    Posted 03-28-2018 02:04 AM
    Hello All,

    I would like to have some part of commands to be executed by a different user other than the user that it is already been logged into. The following command is placed in the PROCESS tab of UNIX job.

    sudo su - <otherUser> bash -c 'service stop all' 

    When I execute the job, it is not showing up any logs in report window, I guess it is looking for password to switch to other user.
    However, I want to switch to the other user without password and also it would be good if I am not advised to modify /etc/sudoers file. :)

    Note: Please look at the following, how I was able to switch to other user manually without entering password and then executed "service stop all" command.  

    [user1@us5np309 ~]$ sudo su - user2
    =======================================
    sourcing...  
    =======================================
    [user2@us5np309 ~]$ service stop all

    Am I missing out something simple here. Please help me with this.

    Regards,
    Srujan.


  • 2.  Switch to other User and execute commands

    Posted 04-03-2018 02:22 PM
    Hi Srujan,

    Would something like this work in your scenario? The below will put the password in as well as execute some shell script (you can replace with your own):

    echo TARGET_USER_PASS | sudo -S -H -u TARGET_USER bash -c 'bash /usr/local/some.sh'