Page 1 of 1

[SOLVED]How to execute script with sudo using session and startup?

Posted: Sat Jan 18, 2020 9:18 pm
by acecombat2
Good Morning,

I need help regarding auto start vm workstation application with running a particular vm machine using script. on login.

Code: Select all

#!/usr/bin/env bash

### BEGIN INIT INFO
# Provides: vmwareautostart
# Required-Start: $vmware $network $syslog
# Required-Stop: $vmware $network $syslog
# X-Start-Before:
# X-Stop-After:
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: This service auto-starts and stops VMware guests
### END INIT INFO

# To use this script, follow installation instructions at
# http://www.atrixnet.com/autostart-vmware-virtual-machines-on-boot-in-linux
# ...and then customize it below

# ======== USER CONFIGURABLE VARIABLES (CUSTOMIZE THESE PLEASE) ==========

# unless you have weird characters or shell escapes in your varable values
# there is no need to muddy up the shell code by using excessive quotes
# and punctuation.  For this reason, you'll see the example variable values
# below are simple and clean.  Don't put spaces between variables, values,
# and equal signs (=).  You can't do that in shell scripts.

# number of seconds to wait between startup of multiple VMs.  The faster
# your disk storage, the lower this number can be.  The idea is to not
# start more VMs at one time than your system can handle and still
# remain stable/responsive

VM_wait_between=30

# max number of seconds to wait for a virtual machine to gracefully shut
# down before taking it down hard.  Allow more time for app servers and
# windows virtual machines.

VM_max_stop_wait=30

# name the system user who runs your virtual machines.  you should not be
# running virtual machines as root, in the event that one gets compromised
# that could be a security liability.  I recommend that you consider
# creating an unprivileged system account that does nothing else but run
# virtual machines in vmware

VM_user=user

# list your virtual machines below, with each on its own line within the
# perenthesis block, as shown.  Make sure each VM is a fully-qualified
# path to the .vmx file for the virtual machine you want to auto-start

VM_list=(
   /media/Storage_OMV/virtualdirectory/Windows10x64/Windows10x64.vmx
);

# ======== THE REST OF THIS CODE IS NOT CONFIGURABLE ==========

export PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin

if [[ $( id -u ) -ne 0 ]];
then
   echo You must run this script as root or with sudo

   exit 1
fi
if [[ "$( getent passwd $VM_user )" == "" ]];
then
   echo Could not locate specified VM user "'$VM_user'" on the system.  Abort.

   exit 1
fi

VM_user_group=$( id -gn $VM_user );

VM_cmd_exec="sudo -u $VM_user -g $VM_user_group vmrun"

case "$1"
in
   start)
      VM_iter=0
      VM_list_length=${#VM_list[@]};

      for vm in "${VM_list[@]}";
      do
         if [[ $( vmrun list 2>/dev/null | grep $vm | wc -l ) -ne 0 ]];
         then
            echo VM "$vm" is already running

            continue;
         fi

         echo Starting up VM "$vm" ...

         $VM_cmd_exec start "$vm" nogui >/dev/null 2>&1

         VM_iter=$(( $VM_iter + 1 ));

         if [[ $VM_iter -lt $VM_list_length ]];
         then
            echo -n ...waiting $VM_wait_between seconds before starting next VM

            for tick in $( seq 1 $VM_wait_between );
            do
               echo -n .

               sleep 1
            done

            echo
         fi
      done

      $0 status
   ;;

   stop)
      for vm in "${VM_list[@]}";
      do

         if [[ $( vmrun list 2>/dev/null | grep "$vm" | wc -l ) -eq 0 ]];
         then
            echo VM "$vm" is not running

            continue;
         fi
         echo Stopping "$vm"...

         $VM_cmd_exec stop "$vm" soft >/dev/null 2>&1 &

         VM_stop_pid=$!

         VM_stop_waited=0;

         echo -n ...Waiting $VM_max_stop_wait seconds for it to stop

         while kill -0 $VM_stop_pid >/dev/null 2>&1 ;
         do
            echo -n .

            sleep 1

            VM_stop_waited=$(( $VM_stop_waited + 1 ));

            if [[ $VM_stop_waited -gt $VM_max_stop_wait ]];
            then
               echo
               echo -n ...Timeout reached while waiting for graceful shutdown.
               echo -n Hard shutdown forced...

               $VM_cmd_exec stop "$vm" hard >/dev/null 2>&1;
            fi
         done

         echo
         echo ...VM "$vm" stopped.
      done

      $0 status
   ;;

   status)
      $VM_cmd_exec list
   ;;

   restart)
      $0 stop && $0 start
   ;;

   *)
      echo Usage: $0 '{start|stop|status|restart}'

      exit 1
   ;;

esac

# vim: set ft=sh :
The script works fine when execute via terminal which asking for user password when using sudo.

I'm stuck since the command I've searched via google search won't work.

The behavior I'm seeing as soon as the command execute within session and startup it immediately exit. :confused:

Code: Select all

echo mypassword | sudo -S /home/user/Documents/Apps/vmautostart.sh start
can anyone help me point the right direction? I can't find any similar use case from search engine.

Re: How to execute script with sudo using session and startup?

Posted: Sat Jan 18, 2020 9:44 pm
by JayM
I don't know whether it may lead to a possible security issue but I found this site via searching the web (using Duckduckgo) for the keywords

Code: Select all

auto run command as sudo
https://linuxize.com/post/how-to-run-su ... -password/
What you would need to do is edit /etc/sudoers using visudo and set up sudo so just your script can be run without requiring your password, but passwords are still required for everything else.

EDIT: even better, edit /etc/sudoers.d/antixers as root and add the information there rather than editing /etc/sudoers. There are already several instances of commands that can run without password entry being required within that antixers file. Just add your script to them.

Re: How to execute script with sudo using session and startup?

Posted: Sat Jan 18, 2020 10:42 pm
by dolphin_oracle
that looks like a sysVinit script.

have you tried enabling the init script at bootup?

Code: Select all

sudo update-rc.d path_to_init_script defaults
by convention init scripts are usually kept in /etc/init.d

Re: How to execute script with sudo using session and startup?

Posted: Sat Jan 18, 2020 11:42 pm
by acecombat2
dolphin_oracle wrote: Sat Jan 18, 2020 10:42 pm that looks like a sysVinit script.

have you tried enabling the init script at bootup?

Code: Select all

sudo update-rc.d path_to_init_script defaults
by convention init scripts are usually kept in /etc/init.d
am using systemd as default init system. Due to installation of jellyfin. Thus, looking for alternative like session and startup.

I'm going to try JayM suggestion.

Re: [SOLVED]How to execute script with sudo using session and startup?

Posted: Sun Jan 19, 2020 2:44 am
by acecombat2
JayM Suggestion works except ended up follow the tutorial link create own sudoer.d file.

Don't want to mess with antixxers file.

Then proceed create start up command using session and startup normally without password

Code: Select all

sudo /path/to/script start
Thanks JayM & dolphin_oracle

Re: [SOLVED]How to execute script with sudo using session and startup?

Posted: Sun Jan 19, 2020 2:46 am
by JayM
I didn't think of creating a new file in /.etc/sudoers.d. Good job!

Re: [SOLVED]How to execute script with sudo using session and startup?

Posted: Sun Jan 19, 2020 5:57 am
by Head_on_a_Stick
Less hacky solution: http://0pointer.de/blog/projects/system ... ins-3.html ← convert the init script to a unit file.

Re: [SOLVED]How to execute script with sudo using session and startup?

Posted: Sun Jan 19, 2020 6:48 am
by fehlix
Head_on_a_Stick wrote: Sun Jan 19, 2020 5:57 am Less hacky solution: http://0pointer.de/blog/projects/system ... ins-3.html ← convert the init script to a unit file.
IIRC, in MX-19 installed init.d scripts will automatically generated and setup systemd service files.

Re: [SOLVED]How to execute script with sudo using session and startup?

Posted: Sun Jan 19, 2020 6:53 am
by Head_on_a_Stick
fehlix wrote: Sun Jan 19, 2020 6:48 am in MX-19 installed init.d scripts will automatically generated and setup systemd service files.
Yes, the adherence to LSB should allow the init script to be used natively even with systemd as PID1.

Re: How to execute script with sudo using session and startup?

Posted: Sun Jan 19, 2020 10:17 am
by acecombat2
dolphin_oracle wrote: Sat Jan 18, 2020 10:42 pm that looks like a sysVinit script.

have you tried enabling the init script at bootup?

Code: Select all

sudo update-rc.d path_to_init_script defaults
by convention init scripts are usually kept in /etc/init.d
fehlix wrote: Sun Jan 19, 2020 6:48 am
Head_on_a_Stick wrote: Sun Jan 19, 2020 5:57 am Less hacky solution: http://0pointer.de/blog/projects/system ... ins-3.html ← convert the init script to a unit file.
IIRC, in MX-19 installed init.d scripts will automatically generated and setup systemd service files.
If I understood correctly, following dolphin_oracle instruction automatically the script will run as part of systemd boot sequence? I'm still in learning curve albeit, slowly.

Re: How to execute script with sudo using session and startup?

Posted: Sun Jan 19, 2020 11:24 am
by Head_on_a_Stick
acecombat2 wrote: Sun Jan 19, 2020 10:17 am If I understood correctly, following dolphin_oracle instruction automatically the script will run as part of systemd boot sequence?
Actually you can just use systemctl to enable it.

I saved your script to /etc/init.d/vbox and made it executable then:

Code: Select all

empty@mx:~
$ sudo systemctl enable --now vbox
vbox.service is not a native service, redirecting to systemd-sysv-install.
Executing: /lib/systemd/systemd-sysv-install enable vbox
empty@mx:~
$ systemctl is-enabled vbox
vbox.service is not a native service, redirecting to systemd-sysv-install.
Executing: /lib/systemd/systemd-sysv-install is-enabled vbox
enabled
empty@mx:~
$
Seems to work :-)

Re: How to execute script with sudo using session and startup?

Posted: Sun Jan 19, 2020 5:35 pm
by fehlix
acecombat2 wrote: Sun Jan 19, 2020 10:17 am If I understood correctly, following dolphin_oracle instruction automatically the script will run as part of systemd boot sequence? I'm still in learning curve albeit, slowly.
If you installed the sysvinit (init.d) script when booted with sysvinit - not with systemd -, the systemd entries will also be generated to be available when booted with systemd.

Re: How to execute script with sudo using session and startup?

Posted: Sun Jan 19, 2020 6:48 pm
by acecombat2
Head_on_a_Stick wrote: Sun Jan 19, 2020 11:24 am
acecombat2 wrote: Sun Jan 19, 2020 10:17 am If I understood correctly, following dolphin_oracle instruction automatically the script will run as part of systemd boot sequence?
Actually you can just use systemctl to enable it.

I saved your script to /etc/init.d/vbox and made it executable then:

Code: Select all

empty@mx:~
$ sudo systemctl enable --now vbox
vbox.service is not a native service, redirecting to systemd-sysv-install.
Executing: /lib/systemd/systemd-sysv-install enable vbox
empty@mx:~
$ systemctl is-enabled vbox
vbox.service is not a native service, redirecting to systemd-sysv-install.
Executing: /lib/systemd/systemd-sysv-install is-enabled vbox
enabled
empty@mx:~
$
Seems to work :-)
Nifty! Thank you for showing to me of how to do it. am not ready yet to delve in hacking sysvinit shell script into systemd unit.

fehlix wrote: Sun Jan 19, 2020 5:35 pm
acecombat2 wrote: Sun Jan 19, 2020 10:17 am If I understood correctly, following dolphin_oracle instruction automatically the script will run as part of systemd boot sequence? I'm still in learning curve albeit, slowly.
If you installed the sysvinit (init.d) script when booted with sysvinit - not with systemd -, the systemd entries will also be generated to be available when booted with systemd.
Got it. Now we have 3 options to go about it.

I'm going to try with these alternatives.