rsync doesn't work from cron with custom "--files-from" parameter - ssh

I have the script that works perfectly in case of starting from terminal, but it doesn't work from cron
#!/bin/bash
echo $(date) Starting...
rsync -avR --files-from=<(ssh -i /root/.ssh/id_rsa root#hostA 'find /Data/for_mk/* -type f -cmin -160') root#hostA:/ /
crontab:
SHELL=/bin/bash
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
*/2 * * * * sh /opt/script.sh >> /var/log/rsync 2>&1
log:
rsync: failed to open files-from file <(ssh -i /root/.ssh/id_rsa root#hostA find /Data/for_mk/* -type f -cmin -160): No such file or directory
rsync error: syntax or usage error (code 1) at main.c(1435) [client=3.0.9]
What I'm doing wrong?
Thanks in advance.

Try removing sh from:
*/2 * * * * sh /opt/script.sh >> /var/log/rsync 2>&1
Or replace it with bash

Related

Why does the PID change when the find command is issued?

I am studying and making a timeout command that exists in CentOS.
I run the sleep and find commands in the "timeout_bash.sh" file.
However, when the find command is executed, the "timeout_bash.sh" file appears to have been executed by the find command in the process list.
Can you tell me why?
Images URL : https://i.stack.imgur.com/t1U9l.jpg
#script File Name : timeout_bash.sh
#!/bin/bash
timeout_bash() {
scriptName="${0##*/}"
declare -i DEFAULT_TIMEOUT=5
declare -i DEFAULT_INTERVAL=1
declare -i DEFAULT_DELAY=1
declare -i timeout=DEFAULT_TIMEOUT
declare -i interval=DEFAULT_INTERVAL
declare -i delay=DEFAULT_DELAY
while getopts ":t:i:d:" option; do
case "$option" in
t) timeout=$OPTARG ;;
i) interval=$OPTARG ;;
d) delay=$OPTARG ;;
esac
done
shift $((OPTIND - 1))
if (($# == 0 || interval <= 0)); then
echo "Not Enter the Time. So not to execute timeout command"
exit 1
fi
# kill -0 pid Exit code indicates if a signal may be sent to $pid process.
(
((t = timeout))
while ((t > 0)); do
sleep $interval
kill -0 $$
((t -= interval))
done
# Be nice, post SIGTERM first.
# The 'exit 0' below will be executed if any preceeding command fails.
kill -s SIGTERM $$ && kill -0 $$
sleep $delay
kill -s SIGKILL $$
) 2> /dev/null &
exec "$#"
}
sleep 5
timeout_bash -t 60 find / -nouser -print 2>/dev/null >> a.txt
timeout_bash -t 60 find / -nogroup -print 2>/dev/null >> b.txt
timeout_bash -t 60 find / -xdev -perm -2 -ls | >> c.txt
timeout_bash -t 60 find / -xdev -name '.*' -print >> d.txt &

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

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

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.

Rsync over ssh with key gets error EC2

I can ssh into my EC2 with:
ssh -i /Users/User/Downloads/key.pem ubuntu#ec2-myec2.amazonaws.com
My directory:
/A
/B
/folderToTransfer
I can rsync into the same directory with:
rsync -avrz -e “ssh -i /Users/User/Downloads/key.pem” /
/Users/User/Documents/Programming/A/B/folderToTransfer /
ubuntu#ec2-myec2.amazonaws.com
New directory:
/A
/B
/folderToTransfer
/ubuntu#ec2-myec2.amazonaws.com
But this fails (when adding :~/ to the end)
rsync -avrz -e “ssh -i /Users/User/Downloads/key.pem” /
/Users/User/Documents/Programming/A/B/folderToTransfer /
ubuntu#ec2-myec2.amazonaws.com:~/
With the error
rsync: Failed to exec ?\#200\#234ssh: No such file or directory (2)
rsync error: error in IPC code (code 14) at /BuildRoot/Library/Caches/com.apple.xbs/Sources/rsync/rsync-47/rsync/pipe.c(86) [sender=2.6.9]
rsync: connection unexpectedly closed (0 bytes received so far) [sender]
rsync error: error in rsync protocol data stream (code 12) at /BuildRoot/Library/Caches/com.apple.xbs/Sources/rsync/rsync-47/rsync/io.c(453) [sender=2.6.9]
Others don't seem to have this same problem when rsyncing with ssh -i, what have I done wrong?
rsync -avrz -e “ssh -i /Users/User/Downloads/key.pem” /
^ ^
You're not using the ASCII double quote character " here. You're using some kind of open- and close-quote characters intended for typesetting. Your command is failing because the shell doesn't treat these characters as quote marks; rsync ends up trying to execute a program named “ssh.
Replace the characters with ASCII double quotes:
rsync -avrz -e "ssh -i /Users/User/Downloads/key.pem" /
The AWS ec2 solutions (with keys) below work for me only with a good formatting of the command line
solution 1
rsync -avz -e "ssh -i /root/xxxxxx.pem" ec2-user#xxxxxx.com:/var/www/ /var/www/copywww/
solution 2
rsync -avrz -e " ssh -i /root/xxxxxx.pem " ec2-user#xxxxx.com:/var/www/ /var/www/copywww

SQLCMD Usage in Batch file

Hi All I am Writing a Batch Script Which has to read a set of SQL Files which exists in a Folder then Execute Them Using SQLCMD utiliy.
When I am Trying to execute it does not create any output file. I am not sure where I am wrong and I am not sure how to debug the script. Can someone help me out with script?
#echo off
FOR %F IN (C:\SQLCMD*.SQL) DO sqlcmd -S LENOVO-C00 -U yam -P yam!# -i %F -o C:\SEL.txt -p -b
IF NOT [%ERRORLEVEL%] ==[0] goto get_Error
:Success echo Finished Succesffuly exit /B 0 goto end
:get_error echo step Failed exit /B 40
:end
If this is actually in a batch file (not being executed by hand at the command line), then the %F variables need to have the % characters doubled-up because of the way that cmd.exe executes the lines read from the batch file:
FOR %%F IN (C:\SQLCMD*.SQL) DO sqlcmd -S LENOVO-C00 -U yam -P yam!# -i %%F -o C:\SEL.txt -p -b
Though I would have thought you'd get a
F was unexpected at this time.
error if you only had one % character in front of the variable name.
Try to right click the Batch File and run as administrator.
Seems like the answer would be more like this:
FOR %%F "usebackq" IN (`dir.exe C:\SQLCMD*.SQL`) DO ...