Cron jobs notify


xhaloz

Recommended Posts

Hello,

 

I've managed to get my cron job halfway working.  As in it performs an rsync task. However I would like to get notified via email of all the files that were moved (a list) as well as the status of the job.  

 

Currently I have

/usr/local/emhttp/webGui/scripts/notify -i normal -s "Operation complete"


However what if the operation fails?  What are the different handlers here?  Finally....do I put this command at the end of my script and it covers all the operations above it (pass/fail) ?  or do I need to put it below each rsync task I have in the cron job?

Sorry for the intricate question, reddit isn't at the level of cron jobs yet haha.

Link to comment
13 minutes ago, bonienl said:

The notify command is used to send notifications using the unRAID system. Your code needs to call the notify script for all different conditions and supply the correct parameters to reflect those conditions.

 

 

So I'm pretty new to unRAID.  I successfully can receive an email with the script I used above.  How would I even begin to pass rsync parameters to notify?  I mean do you have like a screenshot example?  If I used rsync -av FileDirectory1 FileDirectory2 what parameters can be passed to notify?

Link to comment

When you call notify without parameters , you'll get the available options.

# notify
notify [-e "event"] [-s "subject"] [-d "description"] [-i "normal|warning|alert"] [-m "message"] [-x] [-t] [add]
  create a notification
  use -e to specify the event
  use -s to specify a subject
  use -d to specify a short description
  use -i to specify the severity
  use -m to specify a message (long description)
  use -x to create a single notification ticket
  use -t to force send email only (for testing)
  all options are optional

notify init
  Initialize the notification subsystem.

notify smtp-init
  Initialize sendmail configuration (ssmtp in our case).

notify get
  Output a json-encoded list of all the unread notifications.

notify archive file
  Move file from 'unread' state to 'archive' state.

Your script can you check the exit code of rsync and use that to call notify. For example

rsync -av FileDirectory1 FileDirectory2
if [[ $? -eq 0 ]]; then
  /usr/local/emhttp/webGui/scripts/notify -i normal -s "Operation complete"
else
  /usr/local/emhttp/webGui/scripts/notify -i warning -s "Operation failed"
fi

 

  • Like 2
  • Thanks 1
Link to comment
4 hours ago, xhaloz said:

That is a language I am unfamiliar with.  How would an average unRAID user even know about that?

 

I'm not really an expert on scripts, but I've been wanting to use something like that for a while, basically most commands have an exit code, for rsync if the exit code is 0 it means it ran successfully, different than 0 there was an error, the script just reads that exit code ($?) and if it's 0 triggers an rsync complete notification, if different than 0 triggers a failed notification, you just need to adapt the rsync command line to your needs and e.g. use the user scripts plugin to run it manually or on a schedule.

Link to comment
20 hours ago, johnnie.black said:

 

I'm not really an expert on scripts, but I've been wanting to use something like that for a while, basically most commands have an exit code, for rsync if the exit code is 0 it means it ran successfully, different than 0 there was an error, the script just reads that exit code ($?) and if it's 0 triggers an rsync complete notification, if different than 0 triggers a failed notification, you just need to adapt the rsync command line to your needs and e.g. use the user scripts plugin to run it manually or on a schedule.

 

Ah ok beautiful! So Johnnie where did you learn that $? mean something?  I currently am using a tool called exiftool which is part of the nerd pack.  This tool organizes all my photos on a weekly basis.  So any photos I've taken on my phone get put in a folder and the script runs sorting all of them by date etc.  Would that $? work for that as well you think?

Link to comment
2 hours ago, johnnie.black said:

Not sure, you can type echo $? after a command to display the last exit code, but I'm not sure if there's no exit code from that command if it will show the exit code from the previous command, if you can get the script to error you can check if it gives a non 0 code.



Ok exiftool replied with a 0 after I ran a script and put echo $? and a 1 when the script errored.  Dude you're a genius!!!!

Link to comment

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.