Why does this script running su never seem to terminate if I change user inside the script?

I have a really simple bash script with 5 commands running under the root user. If I run commands manually one by one everything works - no problem. But as I run it as my-deploy.sh file via command

bash /root/custom-scripts/deploy/my-deploy.sh 2>> /var/log/www-deploy/tatrytec.eu.log

it seems like endless process. Here is the script:

#!/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

composer install  --no-scripts

npm install --production

It starts to run and I can see result of git pull in terminal. But then it dies without any error and it is still running. I can stop it via ctrl+Y. I thing something is wrong with that user but as I wrote before if I run commands one by one it works. I dont understand. Can somebody tell me what could be the problem? Thanks.


Solution 1:

  1. su vlado will need a password unless run as root.
  2. su vlado will wait for input
  3. commands after su valdo are not executed inside su, but after exiting su 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 to su vlado

Solution 2:

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.

Solution 3:

You seem to want to run the last lines in your script as the user vlado. There is a clean way to do that:

sudo -u vlado  your_command 

So the last few lines of your script will look like this:

sudo -u vlado composer install --no-scripts
sudo -u vlado npm install --production

Solution 4:

I guess su vlado needs a password to be typed in.

You could use sudo and make it not need a password for specific commands by modifying /etc/sudoers