ssh on remote server with nohup - not able to create files - ssh

I'm calling a shell script on a remote server with ssh ssh user#host '/dir/myscript.sh'and myscript.sh then runs another script with nohup, but the 2nd script isn't writing to nohup / to any other file with echo "" > test.txt.
myscript.sh
#!/bin/bash
shopt -s expand_aliases
source /home/user/.bash_profile
nohup /dir/myscript-real.sh > my.out 2> my.err < /dev/null &
myscript-real.sh
#!/bin/bash
shopt -s expand_aliases
source /home/user/.bash_profile
echo -e "test"
sleep 15
echo "2nd test"
echo "test" > test.out
exit
When ran via SSH - no file is being created (my.out, my.err or test.out), but when running myscript.sh directly - those 3 files are being created.
Any ideas where is the problem?
Thanks.

Related

release scp connection when no response from server

I have to collect measurement files from different servers, so I used scp command to retrieve them.
But in case the distant server is hanged or no response, I need to close the connection and put a 0 in my measurement file.
Is there any option in scp command allow me to close the connection after 10 seconds for example?
for serv in $SERV_LIST
do
echo "--- Working on server: $serv ---"
trc_file=`ssh user#$serv "$(typeset -f collectSTATS); collectSTATS $serv $DATE $LastRunTime
scp user#$serv:/tmp/result_rechHM2_$serv.tmp /home/voms/HDB2/result_rechHM2_$serv.tmp > /dev/null 2>&1
deleteFile=`ssh voms#$serv "rm /tmp/result_rechHM2_$serv.tmp 2> /dev/null"`
if [ -f /home/voms/HDB2/result_rechHM2_* ]
then
cat /home/voms/HDB2/result_rechHM2_* >> /home/voms/HDB2/TraceRecharge.log
rm -rf /home/voms/HDB2/result_rechHM2_*
fi
done
When ssh or scp command fail with no response, I need to wait only 10 seconds.
we just use the ssh -o ConnectTimeout=5.
it resolve my problem

SSH commands work in CMD but not all in batch

I hope you can help me.
This is the line in CMD:
putty -ssh -P 22 root#192.168.10.100 -i "C:\Users\RNA\MyKey.ppk" -m "C:\Users\RNA\plink_cmds.txt"
All the commands in my batch file work fine except for 'ls'. Why is that?
ls command will throw a sh: ls: cannot execute - No such file or directory.
This is my script:
hostname
cd /tmp
echo '** '
pwd
echo '**'
ls
read

Pass arguments for SQL statement in a shell file from another shell file through ssh command

I am passing command line arguments to a shell file i.e assignRole.sh which contains an SQL command which will use these arguments like below
ssh -o StrictHostKeyChecking=no -T $key < /oracle/oracle_user/makhshif/./assignRole.sh name open_mode >> /oracle/oracle_user/dftest.txt
This gives me error and does not accept arguments of name and open_mode and gives error, but if I execute the statement outside of ssh command like:
/oracle/oracle_user/makhshif/./assignRole.sh name open_mode
This runs fine
What is the problem with ssh command and how should I adjust these parameters so these can be accepted for the shell script assignRole.sh
< /oracle/oracle_user/makhshif/./assignRole.sh
This commands sends a content of that file to stdin. So obviously it can't process variables that you haven't send to remote machine. Just preprocess your script or create a script on remote machine and call it with arguments
Though it's even easier to pass variables like this:
ssh -o StrictHostKeyChecking=no -T $key "var1=$var1 var2=$var2" < /oracle/oracle_user/makhshif/./assignRole.sh name open_mode >> /oracle/oracle_user/dftest.txt
For example my function for executing update scripts on all cluster nodes:
# functions:
ssh_exec(){
local DESCR="$1"; shift
local SCRIPT="$1"; shift
local hosts=("$#")
echo =================================================
echo = $DESCR
echo = Going to execute $SCRIPT...
read -a res -p "Enter 'skip' to skip this step or press Enter to execute: "
if [[ $res = "skip" ]]
then
echo Skipping $SCRIPT...
else
echo Executing $SCRIPT...
for host in "${hosts[#]}"
do
local cur=${!host}
echo Executing $SCRIPT on $host - $cur...
sshpass -p "$rootpass" ssh -o "StrictHostKeyChecking no" root#${cur} \
"ns1=$ns1 ns2=$ns2 search=$search zoo1=$zoo1 zoo2=$zoo2 zoo3=$zoo3 node0=$node0 pass=$pass CURIP=$cur CURHOST=$host bash -s" \
<$SCRIPT >log-$SCRIPT-$cur.log 2>&1
echo Done.
done
echo =================================================
fi
}
Then I use it like this:
read -p "Please check that Solr started successfully and Press [Enter] key to continue..."
#Solr configset and collections:
ssh_exec "Solr configset and collections" script06.sh zoo1 zoo2 zoo3
This command executes script06.sh on 3 servers (zoo1,zoo2,zoo3)
As Sayan said, using < redirects the output of running the assignRole.sh script locally, but you want to execute that script on the remote host, with the arguments.
Pass the whole command as the final argument to ssh, in quotes:
ssh -o StrictHostKeyChecking=no -T $key "/oracle/oracle_user/makhshif/./assignRole.sh name open_mode" >> /oracle/oracle_user/dftest.txt
or split into multiple lines for readability:
ssh -o StrictHostKeyChecking=no -T $key \
"/oracle/oracle_user/makhshif/./assignRole.sh name open_mode" \
>> /oracle/oracle_user/dftest.txt

Running a script when connecting to server with ssh

I use the kitty terminal emulator, so when I connect to a new server, I (usually) need to ad the terminfo (at least, this way it seems to work). To do this I wrote a script. While I was at it, I added a bit of code to add a public key if the user wants it to.
Not really relevant for the question, but here is the code:
#!/bin/bash
host=$1
ip=$(echo $host | cut -d# -f2 | cut -d: -f1)
# Check if it is a unknown host
if [[ -z $(ssh-keygen -F $ip) ]]; then
# Check if there are any ssh-keys
if [ $(ls $HOME/.ssh/*.pub > /dev/null | wc -l) -ne 0 ]; then
keys=$(echo $( (cd $HOME/.ssh/ && ls *.pub) | sed "s/.pub//g" ))
ssh -q -o PubkeyAuthentication=yes -o PasswordAuthentication=no $host "ls > /dev/null 2>&1"
# Check if the server has one of the public keys
if [ $? -ne 0 ]; then
echo "Do you want to add a SSh key to the server?"
while true; do
read -p " Choose [$keys] or leave empty to skip: " key
if [[ -z $key ]]; then
break
elif [[ -e $HOME/.ssh/$key ]]; then
# Give the server a public key
ssh $host "mkdir -p ~/.ssh && chmod 700 ~/.ssh && echo \"$(cat $HOME/.ssh/$key.pub)\" >> ~/.ssh/authorized_keys"
break
else
echo "No key with the name \"$key\" found."
fi
done
fi
fi
# Copy terminfo to server
ssh -t $host "echo \"$(infocmp -x)\" > \"\$TERM.info\" && tic -x \"\$TERM.info\" && rm \$TERM.info"
fi
It is not the best code, but it seems to work. Tips are ofcourse welcome.
The problem is that I need to run this script every time I connect te a new remote server (or I need to keep track of which server is new, but that is even worse). Is there a way to run this script every time I connect to a server (the script checks if the ip is a known host).
Or is there an other way to do this? Adding the public keys is nice to have, but not very important.
I hope somone can help,
Thanks!
There is a trick to identify that you are using ssh to login on the target machine:
pgrep -af "sshd.*"$USER |wc -l
The above command will count the user's processes using sshd
You can add the above command in the target machine, to test if you are connected via ssh. Add the above command to your .profile or .bash_profile script in the target machine.
So that only if you login via ssh your script will run initiation script on the target machine when you login/connect.
Sample .bash_profile on target machine
#!/bin/bash
# .bash_profile
# Get the aliases and functions
if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi
# User specific environment and startup programs
if [[ $(pgrep -af "sshd.*"$USER |wc -l) -gt 0 ]]; then
your_init_script
fi

Arguments of sh command ignored when using with ssh

In the following command, the first argument of sh command echo hey is ignored:
$ ssh localhost sh -c 'echo hey; echo ho'
ho
Why?
Your commandline is:
ssh localhost sh -c 'echo hey; echo ho'
ssh starts a shell on localhost and passes it the comandline:
sh -c echo hey; echo ho
The shell on localhost sees two commands. Both run fine.
The problem is that the first command is: sh -c echo hey
The option -c tells sh to execute the next argument. The next argument is echo. The extraneous hey argument is ignored.
To fix your problem, either change your quoting or just don't run the redundant shell:
ssh localhost "sh -c 'echo hey; echo ho'"
ssh localhost 'echo hey; echo ho'
The main confusion is probably that ssh concatenates all the non-option arguments it receives into a single string that it passes to the remote shell to execute.