13

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.

Čamo
  • 669
  • 3
  • 8
  • 23

4 Answers4

14
  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
Archemar
  • 662
  • 8
  • 21
  • But if I try to type password it shows the password as plain text and enter does not trigger any action. – Čamo Feb 04 '21 at 12:33
  • Yes you are right. It behaves like new console and I can write commands in there and they are executed. But how can I run other commands under other user? – Čamo Feb 04 '21 at 12:50
  • Can you mention su vlado -c 'composer install --no-scripts' ? – Čamo Feb 04 '21 at 13:04
  • 1
    @Čamo Another option to use `sudo -u vlado composer install --no-scripts`. – Dan Feb 05 '21 at 13:59
10

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.

mchid
  • 42,315
  • 7
  • 94
  • 147
  • The problem is that I change user because composer install can not run as sudo. It throws warning. The script runs as root user. That is the reason why I need to change user and run commands without sudo. – Čamo Feb 04 '21 at 12:42
  • Yes now I understand what happened. Its new shell and I can execute commands there. – Čamo Feb 04 '21 at 13:00
  • 1
    Thanks a lot. Now its working. – Čamo Feb 04 '21 at 13:22
  • @Čamo Yes, when you run `su` with the `-c` option, it runs the command as the user `vlado` and then it exits after running the specified commands. – mchid Feb 04 '21 at 15:09
1

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

mawa
  • 141
  • 6
  • But it should ask me for password. But if I run su vlado as root it does not require password. Are you sure? I could move the script under vlado. But I dont want to do it. – Čamo Feb 04 '21 at 12:05
  • 1
    Didn't notice that you run it as root. The answer from Archemar says that commands after `su vlado` don't get executed inside `su`. I don't know that but if so this could help: `su vlado -c "composer install --no-scripts"` `su vlado -c "npm install --production"` – mawa Feb 04 '21 at 12:19
  • But if I try to type password it shows the password as plain text and enter does not trigger any action. It does not look like it is waiting for password. – Čamo Feb 04 '21 at 12:35
  • The problem is that composer install throws warning if it runs under the super user. That is the reason why I need to change user from root to vlado. – Čamo Feb 04 '21 at 12:37
1

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
Ouss
  • 227
  • 3
  • 11