Error stackscript when using su to the user.
Hello,
I am seeing the following error when booting a linode off of my private stackscript.
The stackscript appears to be error on this line…
su - myuser
The error is…
-su: cannot set terminal process group (582): Inappropriate ioctl for device
This is strange to me because when I created a test shell script using bash after the linode boots, execute that script using that same command there is no error, and I am able to switch users without issue. Is there something in stackscripts that I have to do differently in order to use the command 'su - myuser'? I have also tried 'su myuser' to no avail.
!/bin/bash
3 Replies
I am also able to run su - myuser
in a script, but I receive the same error when calling it in a StackScript. It appears that this is related to job control not being enabled.
In an environment where job control is enabled, you technically can use a statement like that in a bash script - at least in that it may not generate an error - but it should not have the intended result, since subsequent commands would be returned to the user that first called the su
command. In other words, the script would not know to run the commands as the user you switched to, but rather it would execute the su
line and return execution back to the user that called the command on completion. There are 3 ways that I know of to use su
in a script successfully.
#!/bin/bash
# switch user and execute a single command at a time with 'su -c'
su -c myuser 'command'
su -c myuser 'next command'
su -c myuser 'etc...'
# put the commands in a second script, and then invoke that
# script with 'su -c'
su -c myuser 'bash /path/to/script.sh'
# switch user and execute multiple commands, using a here document
# this will run commands from the beginning to the end of the
# document (from '<<CMD' to 'CMD'), and can also be used to automate
# input into commands, as below
su -c myuser <<CMD
command 1
command 2
input for command 2
etc...
CMD
# a more specific example, again using a here document and entering
# input for the user. This code actually will fail, as you cannot
# change a password this way in a here document, but it illustrates
# what I'm saying a bit more clearly
su -c myuser <<CMD
passwd
<new password>
CMD
As a quick side note, if you do wish to change a password from within a here document, you can do it like this
# generate a random password and store it in '$new_password', then
# change the password for 'myuser' to the newly generated password
su -c myuser >>CMD
new_password=$(date +%s|sha256sum|base64|head -c 32)
echo "myuser:${new_password}" | chpasswd
CMD