LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 12-17-2022, 04:56 PM   #16
alex4buba
Member
 
Registered: Jul 2020
Posts: 620

Original Poster
Rep: Reputation: Disabled

Quote:
Originally Posted by michaelk View Post
Check the rsync exit code. 0 means success.
Please, tell me how?
 
Old 12-17-2022, 05:31 PM   #17
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,592

Rep: Reputation: 5880Reputation: 5880Reputation: 5880Reputation: 5880Reputation: 5880Reputation: 5880Reputation: 5880Reputation: 5880Reputation: 5880Reputation: 5880Reputation: 5880
There are many ways...
Code:
#!/bin/bash
rsync -a ....
status=$?
[ $status -eq 0 ] && echo "$(date) rsync completed successfully" >> logfile || echo "$(date) rsync failed" >> logfile

Last edited by michaelk; 12-17-2022 at 05:34 PM.
 
Old 12-17-2022, 06:42 PM   #18
alex4buba
Member
 
Registered: Jul 2020
Posts: 620

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by michaelk View Post
There are many ways...
Code:
#!/bin/bash
rsync -a ....
status=$?
[ $status -eq 0 ] && echo "$(date) rsync completed successfully" >> logfile || echo "$(date) rsync failed" >> logfile
The job runs OK, but I get the following errors

Quote:
/etc/anajob.sh: line 15: syntax error near unexpected token `||'
/etc/anajob.sh: line 15: ` || echo "$(date) rsync failed" >> /home/alexe/Desktop/afolders.txt'
Here is modifies script:

Quote:
#get day of week
dow=$(/usr/bin/date "+%a")

# convert DOW to lower case i.e. Mon to mon
ldow=${dow,,}

/usr/bin/rsync -a /home/alexe/afolders/ /media/alexe/Elements/$ldow/; date > /home/alexe/Desktop/afolders.txt

status=$?
[ $status -eq 0 ] && echo "$(date) rsync completed successfully" >> /home/alexe/Desktop/afolders.txt
|| echo "$(date) rsync failed" >> /home/alexe/Desktop/afolders.txt
Thanks again
 
Old 12-17-2022, 06:46 PM   #19
alex4buba
Member
 
Registered: Jul 2020
Posts: 620

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by alex4buba View Post
The job runs OK, but I get the following errors



Here is modifies script:



Thanks again
Sorry, I figured it out - I had a line break before the 2nd "||"

All good
Thanks for your help
 
Old 12-17-2022, 07:02 PM   #20
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,592

Rep: Reputation: 5880Reputation: 5880Reputation: 5880Reputation: 5880Reputation: 5880Reputation: 5880Reputation: 5880Reputation: 5880Reputation: 5880Reputation: 5880Reputation: 5880
Code:
[ $status -eq 0 ] && echo "$(date) rsync completed successfully" >> /home/alexe/Desktop/afolders.txt
|| echo "$(date) rsync failed" >> /home/alexe/Desktop/afolders.txt
From your post it does not look like the posted code is all on one line.

Code:
#get day of week
dow=$(/usr/bin/date "+%a")

# convert DOW to lower case i.e. Mon to mon
ldow=${dow,,}

/usr/bin/rsync -a /home/alexe/afolders/ /media/alexe/Elements/$ldow/

status=$?
[ $status -eq 0 ] && echo "$(date) rsync completed successfully" >> /home/alexe/Desktop/afolders.txt \
|| echo "$(date) rsync failed" >> /home/alexe/Desktop/afolders.txt
You can add a \ at the end of the line to continue to the next. I forgot to post writing date timestmap at the end of your rsync command should be removed since it is being handled by the test code now.

Last edited by michaelk; 12-17-2022 at 07:38 PM.
 
Old 12-17-2022, 07:33 PM   #21
alex4buba
Member
 
Registered: Jul 2020
Posts: 620

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by michaelk View Post
Code:
[ $status -eq 0 ] && echo "$(date) rsync completed successfully" >> /home/alexe/Desktop/afolders.txt
|| echo "$(date) rsync failed" >> /home/alexe/Desktop/afolders.txt
From your post it does not look like the posted code is all on one line.

Code:
#get day of week
dow=$(/usr/bin/date "+%a")

# convert DOW to lower case i.e. Mon to mon
ldow=${dow,,}

/usr/bin/rsync -a /home/alexe/afolders/ /media/alexe/Elements/$ldow/

status=$?
[ $status -eq 0 ] && echo "$(date) rsync completed successfully" >> /home/alexe/Desktop/afolders.txt \
|| echo "$(date) rsync failed" >> /home/alexe/Desktop/afolders.txt
You can add a \ at the end of the line to continue to the next. I forgot to posted writing date timestmap at the end of your rsync command should be removed since it is being handled by the test code now.
All working fine now
Many thanks
 
Old 12-17-2022, 08:51 PM   #22
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,768

Rep: Reputation: 1193Reputation: 1193Reputation: 1193Reputation: 1193Reputation: 1193Reputation: 1193Reputation: 1193Reputation: 1193Reputation: 1193
This variant will include an eventual output from rsync:
Code:
if
  /usr/bin/rsync -a /home/alexe/afolders/ /media/alexe/Elements/$ldow/ 2>&1
then
  echo "$(date) rsync completed successfully"
else
  echo "$(date) rsync failed"
fi >> /home/alexe/Desktop/afolders.txt
 
Old 12-19-2022, 02:19 PM   #23
computersavvy
Senior Member
 
Registered: Aug 2016
Posts: 3,327

Rep: Reputation: 1481Reputation: 1481Reputation: 1481Reputation: 1481Reputation: 1481Reputation: 1481Reputation: 1481Reputation: 1481Reputation: 1481Reputation: 1481
Quote:
Originally Posted by alex4buba View Post
The above command is producing the following output:

But, there is no indication if the backup worked or not. Is there a way to show this on the same line?

Many thanks
Sure.
add the following after the rsync command on the same line, before the redirect.
Code:
 ; if [ $? -eq 0 ] ; then echo "backup complete $(date)" ; else echo "backup failed $(date)" ; fi
The redirect would now echo your message to the log file.

This way it will always write the line echoed when the backup completes, indicating complete or failure. You can then peruse the log file and see the days when it completed or failed.

There are many other options depending upon why rsync failed and using the messages in $? when it completes you can easily tailor the message written to the actual status.
 
  


Reply

Tags
backup, cron, kde


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] How can I run a Cron job manually or how can I sure my job executed? n00b_noob Linux - Newbie 43 02-24-2021 10:38 PM
Debian daily cron job won't run, but does run in cron.hourly. sandersch Linux - General 7 05-24-2012 01:50 AM
[SOLVED] BASH Script will not get use result of command when run as cron job dahweeds Linux - Newbie 3 07-01-2011 07:01 PM
linux cron job duplicate job question cpthk Linux - Newbie 4 09-11-2009 08:52 PM
adding a perl script to cron.daily / cron.d to setup a cron job CrontabNewBIE Linux - Software 6 01-14-2008 08:16 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 08:23 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration