Hot answers tagged shell
5
I would personally invert your strategy and run the script as a non-privileged user, with sudo used to run the commands requiring root privileges. Is there any specific reason you need to run the script as root?
To answer your question however, you can use the -c flag to run a specific command as a user:
su someuser -c "touch /tmp/file"
Reference: ...
3
Here is a great answer I found on unix.stackexchange:
If one of the variables SSH_CLIENT or SSH_TTY is defined, it's an ssh session.
The login shell's parent process can be checked with ps -o comm= -p $PPID. If it is sshd, it's an ssh session.
if [ -n "$SSH_CLIENT" ] || [ -n "$SSH_TTY" ]; then
SESSION_TYPE=remote/ssh
else
case $(ps -o comm= -p ...
1
Your command tries to create an already existing directory. You should make this shell code more robust:
tmpdirname=tmpdir
test -d "/media/remotebackup/${tmpdirname}" &&
rm -rf "/media/remotebackup/${tmpdirname}"
pushd /media/localbackup/
test -d "$tmpdirname" && { echo Abbruch; exit 1; }
for dir in *; do
test -d ...
1
I don't want to rely on the hypothesis that a particular username exists on the machine.
There are advantages being the superuser... :-)
scriptuser_created=no
scriptuser=myuser
if ! id "$scriptuser" &>/dev/null
adduser --system "$scriptuser"
scriptuser_created=yes
fi
sudo -u "$scriptuser" command1
sudo -u "$scriptuser" command2
sudo -u ...
Only top voted, non community-wiki answers of a minimum length are eligible
