Terminating a script execute with user1 without loop - bash

This is an example of My script. How do i terminate it after it execute. it looping.
#!/bin/bash
#Checks root permission
if [ $(id -u) != "0" ]; then
echo "You must be the superuser to run this script" >&2
su -c "$0 $#"
fi
echo "welcome to script"
cd /var/app
cp index.html index10.html
su john -c 'command1 && command2'
if [ $? -eq 0 ];then
cp index10.html index.html
echo "script exit"
else
echo "error"
fi
exit 1
I believe the when the script executes with user john. the su -c "$0 $#" runs it with root. and when the su command is done.. script continues. Is there any way i restrict script without executing and terminate with echo "scriptexit". I mean without removing the su -c "$0 $#" ? I know if script executed as root user then it will fine.

Put an exit after the first su command (inside the if block).

Related

Why does passing a variable from one bash script to another cause it to fail?

I have been trying to figure this one out for a while. I am trying to automate a few things. I only have rights to edit the scripts I write. I am currently using my script to call another script that I cannot edit, let's call it script.sh
I have tried:
if [[ -n $PASS ]]; then
su -c 'echo "$PASS" | ./script.sh' &
wait $!
else
./script.sh &
wait $!
fi
if [[ -n $PASS ]]; then
echo "$PASS" | ./script.sh &
wait $!
else
./script.sh &
wait $!
fi
if [[ -n $PASS ]]; then
./script.sh <<< $PASS &
wait $!
else
./script.sh &
wait $!
fi
This calls a script I cannot edit:
#!/bin/bash
echo "foo: "
read PASSWORD
echo
echo "foo"
...
if [ ! -f ./config.ini ]; then
./script2.sh ./config.ini
fi
My issue it that script.sh then calls another script, let's say script2.sh, that cats out a config.ini file to be used later in the process. Script2.sh fails to create config.ini correctly. Specifically the command user=$(/usr/bin/who am i | cut -d ' ' -f1) fails to set the variable.
So, 3 scripts deep one command fails. But it works if run manually or if I don't echo $PASS and enter it manually. Any ideas would be greatly appreciated.

Has my bash script crashed?

I have a bash shell script, that should:
1) check for the existence of a file
2) If file exists exit script, otherwise create file
3) Set off a process
4) Check process has run correctly - and send result to a log file
5) Delete file
6) Exit script
if [ -f $PROPERTIES_HC ]
then
# lockfile/propertiesfile exists so exit the script
log --------- lockfile exists so operation cancelled at `date` ---------
exit 1
else
# no lockfile/propertiesfile so continue
# create the lockfile/propertiesfile
input="./$PROPERTIES_VAR"
while IFS= read -r line || [ -n "$line" ]; do
eval "echo $line" >> $PROPERTIES_HC
done < $PROPERTIES_VAR
#Run Process
RUN_MY_PROCESS --overridefile $PROPERTIES_HC >> $LOG_FILE
#Check Process Ran Okay
if [ "$?" = "0" ]; then
echo "RAN WITHOUT ERROR" >> $LOG_FILE
else
echo "SOME ERROR!" >> $LOG_FILE
fi
# Remove the lockfile/propertiesfile
rm -rf $PROPERTIES_HC
fi
This script seemed to have been running fine, however recently I came across a situation where the "RUN_MY_PROCESS" element of the script failed, and the script seems to have simply exited leaving the lockfile in place.
As I understand it unless I set something like #!/bin/sh -e, the script should run on regardless of errors. Have I misunderstood how shell scripts/shell error handling work (I am new to this!), or is it that my shell script has crashed itself - hence it didn't finish running?
Thanks in advance for any help.
The proper way to handle errors inside your script (i.e. errors that cause your script to crash) is through traps.
You could modify your script as follow :
if [ -f $PROPERTIES_HC ]
#your regular script here
#...
#Run Process
trap 'echo "SOME ERROR" >> $LOG_FILE && rm -rf $PROPERTIES_HC' ERR
RUN_MY_PROCESS --overridefile $PROPERTIES_HC >> $LOG_FILE
#rest of your script here
#....

Unable to kill remote processes with ssh

I need to kill remote processes with a shell script as follows:
#!/bin/bash
ip="172.24.63.41"
user="mag"
timeout 10s ssh -q $user#$ip exit
if [ $? -eq 124 ]
then
echo "can not connect to $ip, timeout out."
else
echo "connected, executing commands"
scp a.txt $user#$ip://home/mag
ssh -o ConnectTimeout=10 $user#$ip > /dev/null 2>&1 << remoteCmd
touch b.txt
jobPid=`jps -l | grep jobserver | awk '{print $1}'`
if [ ! $jobPid == "" ]; then
kill -9 $jobPid
fi
exit
remoteCmd
echo "commands executed."
fi
After executed it I found the scp and touch clauses had been executed, but the kill clause had not been executed successful and the process is still there. If I run clauses from "jobPid= ..." to "fi" on remote machine the process can be killed. How to fix it?
I put a script on the remote machine which can find and kill the process, then I ran the script on local machine which execute the script on the remote machine with ssh. The script is as follows:
Local script:
#!/bin/bash
ip="172.24.63.41"
user="mag"
timeout 10s ssh -q $user#$ip exit
if [ $? -eq 124 ]
then
echo "can not connect to $ip, timeout out."
else
echo "connected, executing commands"
ssh -q $user#$ip "/home/mag/local.sh"
echo "commands executed."
fi
remote script:
#!/bin/bash
jobPid=`jps -l | grep jobserver | awk '{print $1}'`
if [ ! $jobPid == "" ]; then
kill -9 $jobPid
fi
Your script needs root access (WHICH IS NEVER A GOOD IDEA). Or make sure your program which is running, is running under your webuser/group

How do I sudo if in Bash?

I need to check for a directory in the home directory of another user. Normally I would sudo but that forks another process and I also lose my environment.
For example, I have:
if [[ -d "/home/otheruser/svn" ]];
then
echo "SVN exists"
else
echo "SVN does not exist"
fi
I need the the test condition to run with root permissions.
if sudo test -d "/home/otheruser/svn"; then
You need to run it under a subshell. Example:
if sudo bash -c '[[ -d "/home/otheruser/svn" ]]'
then
echo "SVN exists"
else
echo "SVN does not exist"
fi

How do I determine if a shell script is running with root permissions?

I've got a script I want to require be run with su privileges, but the interesting scripted command that will fail comes very late in the script, so I'd like a clean test up front to determine if the scrip will fail without SU capabilities.
What is a good way to do this for bash, sh, and/or csh?
bash/sh:
#!/usr/bin/env bash
# (Use #!/bin/sh for sh)
if [ `id -u` = 0 ] ; then
echo "I AM ROOT, HEAR ME ROAR"
fi
csh:
#!/bin/csh
if ( `id -u` == "0" ) then
echo "I AM ROOT, HEAR ME ROAR"
endif
You might add something like that at the beginning of your script:
#!/bin/sh
ROOTUID="0"
if [ "$(id -u)" -ne "$ROOTUID" ] ; then
echo "This script must be executed with root privileges."
exit 1
fi

Resources