Menu

sudo su - in shell job not taken

Help
Chico
2025-12-17
2026-01-08
  • Chico

    Chico - 2025-12-17

    Hi

    My agent is install as user js7 on a server.
    this user is allowed in sudoers to sudo without asking a password

    made a test job with this script

    #!/bin/bash
    whoami
    sudo su - anotherone
    whoami
    exit
    

    result should be
    js7
    anotherone

    but, after running the result is
    js7
    js7

    commadn sudo su seems to execute fine, but did not changed user.
    how can i tell my shell to go to different user in order to executer shell owned by this user ?

     
  • Andreas

    Andreas - 2025-12-19

    Hi Chico,

    sudo works for a single command when used in scripts. This is a shell limitation and is not related to JS7.

    Your job script should use multiline input like this:

    sudo -su anotherone <<EOF
    whoami
    pwd
    EOF
    

    A practical approach is use of JS7 - Script Includes to hide sudo commands from job scripts:

    ##!include sudo-begin
    pwd
    whoami
    sleep 30
    ##!include sudo-end 
    

    The sudo-begin Script Include holds the line: sudo -su anotherone <<EOF
    The sudo-end Script Include holds the line: EOF

     
  • Chico

    Chico - 2026-01-08

    Yeah figure this out later i could do this, fogot the EOF kind of way
    as i'm only lauching already existing ksh script, i did a
    sudo su - <user> -c "/path/to/script.ksh"
    and it works fine as well
    thanks.

     

Log in to post a comment.

MongoDB Logo MongoDB