Why does this script running su never seem to terminate if I change user inside the script?
unless run as root.su vlado
will need a passwordsu vlado
will wait for input- commands after
su valdo
are not executed insidesu
, but after exitingsu valdo
what happen
cd /var/www/html/tatrytec.eu # change dir
git pull # pull git repos
# Change user bacause of composer install warrning
su vlado # either ask password or wait for input
composer install --no-scripts # if this get executed, you are no longer as vlado
- the key point is that
su vlado
will fork a new shell, that will ignore (as is) following line in original bash script. - when copy/pasting you don't have that limitation (as you copy inside vlado's new shell)
I try to explain in more details which user/what happen
cd /var/www/html/tatrytec.eu # ROOT change dir
git pull # ROOT pull git repos
# Change user bacause of composer install warrning
su vlado # VLADO wait for input
new shells as VLADO> sample command
new shells as VALDO> exit
composer install --no-scripts # ROOT run composer
proposed correction
as root
#!/bin/bash
# Run this script as bash command like: bash create-apache-site.sh
cd /var/www/html/tatrytec.eu
git pull
# Change user bacause of composer install warrning
su vlado <<EOF
composer install --no-scripts
npm install --production
EOF
where
su vlado << EOF
...EOF
will feed all lines tosu vlado
Use this instead:
#!/bin/bash
# Run this script as bash command like: bash create-apache-site.sh
cd /var/www/html/tatrytec.eu
git pull
# Change user bacause of composer install warrning
su vlado -c 'composer install --no-scripts; npm install --production'
The -c
or --command
option for su
allows you to run a command.