Linux: su error when run as root - su

why following command gives an error exit code, when run as root (tested under SLES11, RedHat 5.1):
srl-suse11-test:/tmp # su - user1 -c 'echo hallo'
srl-suse11-test:/tmp # echo $?
1
Thank in advance

Related

Running "sudo su" within a gitlab pipeline

I've installed some software on a server that my gitlab runner SSH's to, and one of the commands needs to be run after doing sudo su. If I run it as a regular user, but with sudo in front of it - it doesn't work. I have to first completely switch to the sudo user first.
This works fine when I SSH into the server and do the commands manually. But when I try it from the pipeline (rough code below):
my_script:
stage: stage
script:
- ssh -o -i id_rsa -tt user#1.1.1.1 << EOF
- sudo su
- run_special_command <blah blah>
- exit
# above exits from the SSH. below should stop the pipeline
- exit 0
- EOF
I get very weird output like the below:
$ sudo su
[user#1.1.1.1 user]$ sudo su
echo $'\x1b[32;1m$ run_special_command <blah blah>\x1b[0;m'
run_special_command <blah blah>
echo $'\x1b[32;1m$ exit\x1b[0;m'
exit
echo $'\x1b[32;1m$ exit 0\x1b[0;m'
exit 0
echo $'\x1b[32;1m$ EOF\x1b[0;m'
And what I'm seeing is that it doesn't even run the command at all - and I can't figure out why.
In this case, you need to put your script as a multi-line string in your YAML. Alternatively, commit a shell script to repo and execute that.
and one of the commands needs to be run after doing sudo su. If I run it as a regular user, but with sudo in front of it - it doesn't work.
As a side note, you can probably use sudo -E instead of sudo su before the command. But what you have should also work with the multi-line script.
MyJob:
script: |
ssh -o -i id_rsa -tt user#host << EOF
sudo -E my_command
EOF
exit 0
Alternatively, write your script into a shell script committed to the repository (with executable permissions set) and run it from your job:
MyJob:
script: “my_script.sh”

sudo without password is not giving any output

all "myscript.sh" does right now is echo "hello".
[jack#server1 scripts]$ ./myscript.sh
hello
the root user gave me the option to run the script as root without a password but when I do, the script doesn't print the "hello"
[jack#server1 scripts]$ sudo ./myscript.sh
Password:
[jack#server1 scripts]$
what am I doing wrong here? do I need to add some kind of switch ?
I don't see anything about this in man sudo
by the way, obviously I don't have root access to configure anything about this
this is the content of the script:
#!/bin/bash +xv
echo "hello"
and about the permissions and ownership:
[jack#server1 scripts]$ls -l myscript.sh
-rwxrwxr-x 1 jack SiteAdmin 1279 Dec 4 07:54 myscript.sh

Why `uname -a` invoke exit1 for sslocal?

The shadowsocks client was running in my pc.
uname -a
Linux MiWiFi 3.16.0-4-amd64 #1 SMP Debian 3.16.43-2+deb8u2 (2017-06-26) x86_64 GNU/Linux
[1]+ Exit 1 sudo sh -c '/usr/bin/nohup /usr/local/bin/sslocal -c /etc/shadowsocks_racks.json > /var/log/ss.log 2>&1'
Why command uname -a invoke Exit1 for sslocal here?
What does Exit 1 sudo sh -c '/usr/bin/nohup /usr/local/bin/sslocal -c /etc/shadowsocks_racks.json > /var/log/ss.log 2>&1'
mean ?
Why command uname -a invoke Exit1 for sslocal here?
You've completely misinterpreted the output. Before uname ran, a background command running as job 1 exited with a status code of 1. The shell couldn't tell you until a command was run, and now you have been told.

Run command as other user in ubuntu

I try to run command as other user in Ubuntu. But it cannot find the command.
$ sudo su - hbase -c "echo $JAVA_HOME"
/usr/local/jdk
$ sudo su - hbase -c "echo $PATH"
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/usr/local/jdk/bin
$ sudo su - hbase -c "java -version"
-su: java: command not found
$ sudo su - hbase -c "/usr/local/jdk/bin/java -version"
java version "1.8.0_91"
Java(TM) SE Runtime Environment (build 1.8.0_91-b14)
Java HotSpot(TM) 64-Bit Server VM (build 25.91-b14, mixed mode)
I tested in centos, there is no problem. I don't know why? And how can I run java in other user?
My JAVA_HOME is set in /etc/bash.bashrc, and Ubuntu version is ubuntu server 14.04.1.
Actually, I'm trying to start/stop hbase in hadoop user in
$ sudo su - hbase -c "/usr/local/hbase-1.1.5/bin/hbase-daemon.sh stop regionserver"
no regionserver to stop because no pid file /tmp/hbase-hbase-regionserver.pid
And this error is because it cannot get my environment export HBASE_PID_DIR="/data/hadoop/run/hbase", which I set in /etc/bash.bashrc.
I did more tests, first, I added export ENVTEST=hello to /etc/bash.bashrc, then, did the following tests.
$ echo $ENVTEST
$ sudo su - hbase -c "echo $ENVTEST"
$ source /etc/bash.bashrc
$ sudo su - hbase -c "echo $ENVTEST"
hello
It seems like, when you do sudo su - hbase -c "echo $ENVTEST", it is using the my user environment instead of hbase.
it looks like /usr/local/jdk/bin is not in hbase's PATH. add /usr/local/jdk/bin to PATH in /etc/bash.bashrc or /home/hbase/.bashrc.
because of the double-quotes, this test doesn't do what you might think:
sudo su - hbase -c "echo $PATH"
$PATH inside the double-quotes has its value substituted in before sudo su - hbase -c runs.
try these tests out:
sudo su - hbase -c "echo `whoami`"
sudo su - hbase -c 'echo `whoami`'
sudo su - hbase -c 'echo $JAVA_HOME'
sudo su - hbase -c 'echo $PATH'

execute .sh script from cygwin on windows task scheduler

I am trying to execute this .sh script on cygwin on windows machine but receiving this error.
this is the script:
fetchReviewImages.sh
====================
#!/bin/bash
mysql -u root -pjavahot -B -e "SELECT camera_name, id FROM inteliviz.cameras;" --skip-column-names | while read CAM_NAME CAM_ID
do
/xampp/htdocs/getReviewImages.py $CAM_ID
done
Cygwin error:
===============
$ C:/cygwin/bin/sh.exe -l -c "C:/xampp/htdocs/fetchReviewImages.sh"
C:/xampp/htdocs/fetchReviewImages.sh: line 3: mysql: command not found

Resources