Results 1 to 8 of 8

Thread: Script run part of program as user and other part as root

  1. #1
    Join Date
    Jun 2008
    Location
    South-West France
    Posts
    1,169

    Default Script run part of program as user and other part as root

    Hello.

    Config :
    leap_15.2 beta
    laptop asus rog strix
    config script running on real system or on a virtualbox vm Version 6.1.8 r137981 (Qt5.9.7) as well.
    Using NetworkManager

    The script run from usb stick on a system just after fresh install.
    The script begin to source two files for setting some env variables value, and acquire some particular functions.

    The script is started with sudo to be able to write data in some system files.

    I have a problem with a virtualbox function which must be run as the user owning the virtualbox VM.
    I use this command :
    Code:
    sudo runuser -l  user_install -c  'GET_MAC_ADRESS_FROM_VBOX_FILE  'Leap_15.2_APP_1'
    GET_MAC_ADRESS_FROM_VBOX_FILE is the name of an exported function defined in a file sourced by the main running script.
    'Leap_15.2_APP_1' is the name of the VM.
    The problem arise because every things sourced at the beginning are not accessible when 'sudo runuser -l user_install ......' execution.



    Any help is welcome.
    Thanks for helping. JCD
    __________
    server leap 15.0 -- ASUS g75vw KDE leap 15.0 -- ASUS G731GV KDE leap 15.1 -- acer aspire s13 win 10 home -- HP Omen win 10 home - scan EPSON V500 - Brother HL2250DN - Samsung CLP-325W

  2. #2
    Join Date
    Jun 2008
    Location
    Netherlands
    Posts
    27,146

    Default Re: Script run part of program as user and other part as root

    A bit beside your question, why are you still running the beta release where the real version is already available for some time. I assume nobody else will now still have a test version of 15.2, Which makes discussing, in the assumption that all partners use the same thing, will be difficult.
    Henk van Velden

  3. #3
    Join Date
    Jun 2008
    Location
    Netherlands
    Posts
    27,146

    Default Re: Script run part of program as user and other part as root

    And there is an uneven number of ' characters in your command. I do not think that will work.
    Henk van Velden

  4. #4
    Join Date
    Mar 2020
    Location
    São Leopoldo, RS, Brazil
    Posts
    241

    Default Re: Script run part of program as user and other part as root

    Programs like sudo, runuser et al take the name of an executable, at most a shell command. But they can't take a function available only to the parent process (how could they understand?). You might rewrite the command, sourcing the function again, then calling the function. Pass it as a command to bash. Or write a separate bash script. You can prepend env vars to the script command if needed.
    openSUSE Tumbleweed

  5. #5
    Join Date
    Jun 2008
    Location
    South-West France
    Posts
    1,169

    Default Re: Script run part of program as user and other part as root

    Quote Originally Posted by hcvv View Post
    A bit beside your question, why are you still running the beta release where the real version is already available for some time. I assume nobody else will now still have a test version of 15.2, Which makes discussing, in the assumption that all partners use the same thing, will be difficult.
    That doesn’t change the question. Unless the beta version was a fake version.
    Thanks for helping. JCD
    __________
    server leap 15.0 -- ASUS g75vw KDE leap 15.0 -- ASUS G731GV KDE leap 15.1 -- acer aspire s13 win 10 home -- HP Omen win 10 home - scan EPSON V500 - Brother HL2250DN - Samsung CLP-325W

  6. #6
    Join Date
    Jun 2008
    Location
    South-West France
    Posts
    1,169

    Default Re: Script run part of program as user and other part as root

    Quote Originally Posted by awerlang View Post
    Programs like sudo, runuser et al take the name of an executable, at most a shell command. But they can't take a function available only to the parent process (how could they understand?). You might rewrite the command, sourcing the function again, then calling the function. Pass it as a command to bash. Or write a separate bash script. You can prepend env vars to the script command if needed.
    Here a test program saying if a string is hexa or not.
    The called function does nor care who is calling.
    The function is defined in the file sourced at the beginning of execution as said in my question

    Code:
    function make_file_command () {
    
    A_FILE_CMD2="$1"
    
        cat << EOF > "$A_FILE_CMD2"
    
    source /run/media/$LOG_USER/NEW_INSTALL_LINUX/NEW_INSTALL_LINUX/15.x/V07/2020_05_22_12h09/1_NEW_INSTALL_USER/000_CONFIG/001_FIRST_STEP_CONFIG/002_FOLDERS_TO_INSTALL_FIRST/002_01_INSTALLED_BACKUP_SYS_FOLDER/backup_sys/000_COMMON2/Bin/system_common_function
    
    ...................
    ...................
    
    source /run/media/$LOG_USER/NEW_INSTALL_LINUX/NEW_INSTALL_LINUX/15.x/V07/2020_05_22_12h09/001__FUNCTIONS__/new_install_linux_VM_init
    
    source /run/media/$LOG_USER/NEW_INSTALL_LINUX/NEW_INSTALL_LINUX/15.x/V07/2020_05_22_12h09/001__FUNCTIONS__/new_install_linux_VM_init_function
    
    EOF
    
    }
    
    MY_CMD="sudo runuser -l  user_install -c  'is_hexa 01234566789ABCDEF__' "
    
    A_FILE_CMD1="/tmp/000_file_command.sh"
    make_file_command "$A_FILE_CMD1"
    CMD="env | grep BASH_FUNC | sort"
    echo "$CMD"  >>  "$A_FILE_CMD1"
    echo " "  >>  "$A_FILE_CMD1"
    echo "$MY_CMD"  >>  "$A_FILE_CMD1"
    echo " "  >>  "$A_FILE_CMD1"
    echo "RET_CODE=\$?"  >>  "$A_FILE_CMD1"
    echo " "  >>  "$A_FILE_CMD1"
    echo "echo \"RET_CODE=\$RET_CODE\""  >>  "$A_FILE_CMD1"
    echo " "  >>  "$A_FILE_CMD1"
    
    echo .
    echo .
    echo "----------------------------------------------------------------"
    echo "----------------------------------------------------------------"
    echo .
    echo .
    echo "cat \"$A_FILE_CMD1\""
    cat "$A_FILE_CMD1"
    
    bash "$A_FILE_CMD1"
    here is the generated command file :

    Code:
    source /run/media/user_install/NEW_INSTALL_LINUX/NEW_INSTALL_LINUX/15.x/V07/2020_05_22_12h09/1_NEW_INSTALL_USER/000_CONFIG/001_FIRST_STEP_CONFIG/002_FOLDERS_TO_INSTALL_FIRST/002_01_INSTALLED_BACKUP_SYS_FOLDER/backup_sys/000_COMMON2/Bin/system_common_function
    
    source /run/media/user_install/NEW_INSTALL_LINUX/NEW_INSTALL_LINUX/15.x/V07/2020_05_22_12h09/1_NEW_INSTALL_USER/000_CONFIG/001_FIRST_STEP_CONFIG/002_FOLDERS_TO_INSTALL_FIRST/002_01_INSTALLED_BACKUP_SYS_FOLDER/backup_sys/000_COMMON2/Bin/system_common_general_env_var
    
    source /run/media/user_install/NEW_INSTALL_LINUX/NEW_INSTALL_LINUX/15.x/V07/2020_05_22_12h09/001__FUNCTIONS__/new_install_linux_env_var
    
    source /run/media/user_install/NEW_INSTALL_LINUX/NEW_INSTALL_LINUX/15.x/V07/2020_05_22_12h09/001__FUNCTIONS__/new_install_linux_functions
    
    source /run/media/user_install/NEW_INSTALL_LINUX/NEW_INSTALL_LINUX/15.x/V07/2020_05_22_12h09/001__FUNCTIONS__/new_install_linux_VM_init
    
    source /run/media/user_install/NEW_INSTALL_LINUX/NEW_INSTALL_LINUX/15.x/V07/2020_05_22_12h09/001__FUNCTIONS__/new_install_linux_VM_init_function
    
    env | grep BASH_FUNC
     
    sudo runuser -l  user_install -c  'is_hexa 01234566789ABCDEF__' 
     
    RET_CODE=$?
     
    echo "RET_CODE=$RET_CODE"
    and here is the result :

    Code:
    BASH_FUNC_abort_because_test%%=() {  unset A_MSG;
    BASH_FUNC_activate_all_repo_but_source_debug%%=() {  local L_SUSE_VER;
    BASH_FUNC_array_contains_element%%=() {  local e;
    BASH_FUNC_array_to_file%%=() {  NB_PARAM=$#;
    BASH_FUNC_ask_YyNn%%=() {  local L_REPLY="";
    BASH_FUNC_ask_YyNn_timeout%%=() {  local L_REPLY="";
    BASH_FUNC_ask_YyRr%%=() {  local L_REPLY="";
    BASH_FUNC_ask_YyYy%%=() {  local L_REPLY="";
    BASH_FUNC_ask_YyYy_timeout%%=() {  local L_REPLY="";
    BASH_FUNC_config_at_next_reboot%%=() {  local L_REPLY="";
    ...........................
    ........................
    BASH_FUNC_get_subpath_front%%=() {  set +x;
    BASH_FUNC_get_subpath_tail%%=() {  A_PATH="$1";
    BASH_FUNC_is_hexa%%=() {  local -i L_RET_CODE2;
    BASH_FUNC_is_running_in_vm%%=() {  local -i MY_FLAG=0;
    BASH_FUNC_main_repositories_exists%%=() {  local -i NB=0;
    ................................
    ...............................
    BASH_FUNC_message_informal%%=() {  local -a L_ARRAY;
    BASH_FUNC_message_success%%=() {  local -a L_ARRAY;
    BASH_FUNC_message_warning%%=() {  local -a L_ARRAY;
    BASH_FUNC_my_bash_eval%%=() {  local L_SOME_CMD="xxxxxxxxxxxxxxxxxx";
    BASH_FUNC_my_bash_eval_find%%=() {  local L_SOME_CMD="xxxxxxxxxxxxxxxxxx";
    BASH_FUNC_my_bash_eval_rsync%%=() {  local L_SOME_CMD="xxxxxxxxxxxxxxxxxx";
    BASH_FUNC_my_get_salt%%=() {  date --date "$1" +%s%N
    BASH_FUNC_my_rename_ext%%=() {  function my_rename_ext_suite1 () 
    BASH_FUNC_ok_to_do_task%%=() {  local -i L_RET_CODE L_RUN_NO_STOP L_TIMEOUT;
    BASH_FUNC_print_array_full%%=() {  local -i L_NB_ELEMENT;
    BASH_FUNC_print_array_if_debug_on%%=() {  local -i L_DEBUG=0;
    BASH_FUNC_print_asso_array%%=() {  L_ARRAY_NAME="$1";
    BASH_FUNC_print_if_debug_on%%=() {  local -i L_DEBUG;
    BASH_FUNC_read_dummy_operator_input%%=() {  local L_TEMP;
    BASH_FUNC_read_dummy_operator_input_timeout%%=() {  local L_TEMP;
    BASH_FUNC_read_log_on_screen%%=() {  local L_REPLY="";
    .....................
    ....................
    BASH_FUNC_update_param_config_file%%=() {  L_DEBUG=0;
    BASH_FUNC_yes_or_not%%=() {  local L_REPLY="";
    BASH_FUNC_yes_or_not_confirm%%=() {  local -i RETURN_CODE=1;
    
    RET_CODE=112
    RET_CODE=112 is an error because '01234566789ABCDEF__' is not hexa

    So my functions are accessible within "
    sudo runuser -l user_install -c 'is_hexa 01234566789ABCDEF__'
    "

    I shall make another test with my function GET_MAC_ADRESS_FROM_VBOX_FILE which cause problem. Perhaps it is the function itself which has problem.

    I shall give news as soon as possible.

    Thank you for helping.
    Thanks for helping. JCD
    __________
    server leap 15.0 -- ASUS g75vw KDE leap 15.0 -- ASUS G731GV KDE leap 15.1 -- acer aspire s13 win 10 home -- HP Omen win 10 home - scan EPSON V500 - Brother HL2250DN - Samsung CLP-325W

  7. #7
    Join Date
    Jun 2008
    Location
    South-West France
    Posts
    1,169

    Default Re: Script run part of program as user and other part as root

    The os is leap 15.2
    Thanks for helping. JCD
    __________
    server leap 15.0 -- ASUS g75vw KDE leap 15.0 -- ASUS G731GV KDE leap 15.1 -- acer aspire s13 win 10 home -- HP Omen win 10 home - scan EPSON V500 - Brother HL2250DN - Samsung CLP-325W

  8. #8
    Join Date
    Jun 2008
    Location
    South-West France
    Posts
    1,169

    Default Re: Script run part of program as user and other part as root

    Quote Originally Posted by awerlang View Post
    Programs like sudo, runuser et al take the name of an executable, at most a shell command. But they can't take a function available only to the parent process (how could they understand?). You might rewrite the command, sourcing the function again, then calling the function. Pass it as a command to bash. Or write a separate bash script. You can prepend env vars to the script command if needed.
    I didn’t think hard enough.
    Thanks for the explanation.
    Thanks for helping. JCD
    __________
    server leap 15.0 -- ASUS g75vw KDE leap 15.0 -- ASUS G731GV KDE leap 15.1 -- acer aspire s13 win 10 home -- HP Omen win 10 home - scan EPSON V500 - Brother HL2250DN - Samsung CLP-325W

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •