Can't execute script from within rc.local - bash

Within rc.local I have
sudo -H -u myUser -s -- "cd /home/myUser/parlar && /usr/local/bin/meteor &"
I want to test it but when I execute that with
myUser:~$ sudo service rc.local start
/bin/bash: cd /home/myUser/parlar && /usr/local/bin/meteor &: No such file or directory
If I execute the command
cd /home/myUser/parlar && /usr/local/bin/meteor &
it works
How can I execute rc.local so that it changes into the relevant directory, and runs the command as the requested user?

Whatever arguments you give to sudo after -- are considered as command & its arguments.
There is no command/executable named "cd /home/myUser/parlar && /usr/local/bin/meteor". You can however, start bash & run the command within that bash shell.
e.g.
sudo -H -u myUser -s -- bash -c "cd /home/myUser/parlar && /usr/local/bin/meteor &"
Since the first command is cd, this alternate approach may also work:
sudo -H -u myUser -s -i PWD=/home/myUser/parlar -- /usr/local/bin/meteor

To see the log of rc.local itself, it's better to run these commands:
**systemctl restart rc-local.service**
**systemctl status rc-local.service**
May be it can be help full for better trouble shooting

Related

Executing sudo command in bash script without displaying it

I'm executing a command with sudo from bash script, and I'm wondering how to prevent sudo from displaying anything on the screen
echo "mypassword" | sudo -S cp -u /scripts/.bashrc ~/ > /dev/null 2>&1
The result will be an output displaying: [sudo] password for username:
I want to hide that output..
now, before the first comment;
This isn't the safest way, since you're entering your password into the script, but this is strictly internal servers.
Run sudo --help, we can get answer from the parameter list:
-p, --prompt=prompt use the specified password prompt
Then,
echo "mypassword" | sudo -S --prompt="" cp -u /scripts/.bashrc ~/ > /dev/null 2>&1
may do the trick.

Execute multiple commands on remote server using bash

I want to execute cd and scp commands on a remote server which have to be logged in with a different sudo user. Below code snippet asks for the password(echos on screen) for my user but hangs there. It doesn't execute cd
#!/bin/bash
server=myserver.com
ssh $server 'sudo -S -u <user> -i; cd dir1/dir2/; scp file1 user#local-sever'
The issue is that you have a semi colon before cd and so sudo has no command to execute. Remove the ; and it should work:
ssh $server 'sudo -S -u <user> -i scp dir1/dir2/file1 user#local-sever'
There are several ways to address this, but most boil down to wrapping up the commands into a set of instructions. Raman's solution is good since it handles the issue by using full paths, but sometimes that isn't an option. Here's another take -
Assuming your command list can afford the quotes, I like here-strings.
ssh -t sa-nextgen-jenkins.eng.rr.com <<< "
echo 'set -x; cd /tmp; whoami; touch foo; ls -l foo; rm -f foo;'|sudo -iSu user
"
If you need the quotes, try a here-doc.
ssh -t sa-nextgen-jenkins.eng.rr.com <<END
echo 'set -x; echo "$RANDOM"; cd /tmp; whoami; touch foo; ls -l foo; rm -f foo;'|sudo -iSu $user
END
You can also write a small script that has arbitrarily complex commands and scp it over, then use a remote ssh call to execute it as the relevant user.

Need to run chromium as normal user from root script

I have a kiosk that shuts down every day using rtcwake, and this uses root user. I've used && to execute the boot script after rtcwake completes, however it then starts the browser as root causing problems.
This is the command I use:
echo debian | sudo -S rtcwake -m mem -u -t $(date +%s -d '3 days 7:45') && sudo -u debian -i bash $HOME/kiosk/bin/startup.sh &.
The sudo command does work to some extent. It calls the debian user, and executes the correct script, however, it still screws up my chromium preferences.
Here is the startup script:
echo debian | sudo -S hwclock -w
export HOME=/home/debian
#log boot time
echo "Booting at" $(date) >> $HOME/kiosk/bin/logs/boot.log
#echo debian | sudo -S service connman restart
echo debian | sudo -S at 15:30 -f $HOME/kiosk/bin/shutdown.sh
crontab -u debian crontab.txt
bash $HOME/git.sh
#sudo -i -u debian
#start kiosk
export DISPLAY=:0
chromium-browser --kiosk --disable-gpu
http://localhost/kiosk/Client/main.html &
#update ip
bash /home/debian/git.sh &
I'm wondering what could be causing chrome to be executed as root. I have no idea what is going wrong.
If you execute a command with sudo it will not change environment variables like $HOME. Since per user settings are stored in $HOME, this affects the executed program if it needs such configuration files. Check this for example:
sudo -u debian bash -c 'echo $HOME'
It will print the home folder of the calling user, not the home folder of the user specified trough -u. The sudo command supports the -H command line option to handle this, however if it works depends on the security police in use.
As a solution you can use the su command instead of sudo in this case:
... && su debian -c chromium
Since su itself is executed by root you won't be asked for the password.
You must enter a password to log into a new user shell.
The command needs to be modified as follows:
echo debian | sudo -S rtcwake -m mem -u -t $(date +%s -d '3 days 7:45') && echo debian | sudo -S -u debian -i bash $HOME/kiosk/bin/startup.sh &
This avoids needing a password to log in as normal Debian user, and executes the script.

Running bash sript which runs pkill -f using sudo, taking the password automatically

I want to be able run a bash script which calls another command sudo pkill -f "test.py".
How can I do this in a way the password will be taken automatically during command launch?
Is there a way of hiding my password out of being able to see it in the script in some **** manner?
Better don't use any password at all just for a particular root script by using sudo properly. :
all as root :
create a script with :
#!/bin/sh
/usr/bin/pkill -f "test.py"
put it in /path/to/script
chmod 700 /path/to/script
chown root:root /path/to/script
Type visudo as root, then by example :
# /etc/sudoers
# blah...
yourusername ALL = (ALL:ALL) NOPASSWD: /path/to/script
Then,
sudo /path/to/script

Run set of commands as sudo in shell script

I want the following in the middle of my shell script .
---
sudo -u user1
cp /files ./ (as these files are only accessbile to user1)
exit
----
continue as me .
The problem is that till sudo it runs fine, the rest of the commands after sudo aren't executed .
You are using sudo incorrectly. Try:
sudo -u user1 cp /files ./
There's no need to split it into two lines (and indeed doing so is wrong). The "exit" exits your script, so nothing after it is executed.

Resources