Thread: Catch all errors in app

  1. #1
    Registered User
    Join Date
    Apr 2019
    Posts
    6

    Catch all errors in app

    Hi everybody.

    I have made some app that should be executed using crontab once a day.

    I want to receive email in case something going wrong during execution.
    Currently i have only one option. To create bash script that checks value returned by app and compares it to zero. In case it's not equal it sends email.

    Can it somehow bee included to C app?

    Thanks in advance.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,675
    You can internally fork() inside your program to do the same thing.
    Code:
    pid_t p = fork();
    if ( p == 0 ) {
      // all your processing, which may crash
    } else if ( p == (pid_t)-1 ) {
      // fork failed
    } else {
      int status;
      waitpid(p,&status,0);
      if ( WIFSIGNALED(status) ) {
        // send your email
      }
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Feb 2019
    Posts
    1,077
    Do you know how to use SMTP via telnet? Do your SMTP server needs authentication?

  4. #4
    Registered User
    Join Date
    Apr 2019
    Posts
    6
    Quote Originally Posted by flp1969 View Post
    Do you know how to use SMTP via telnet? Do your SMTP server needs authentication?
    Thanks for care
    I use libcurl for this purpose. This is already solved.

  5. #5
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    958
    cron will automatically send an email to the crontab's owner if the program/script returns a non-zero status. So you could keep your program simple and exit with anything but 0 if there's an error (and possibly have your program print an error message to stderr, which gets included in the email from cron).

  6. #6
    Registered User
    Join Date
    Apr 2019
    Posts
    6
    Quote Originally Posted by christop View Post
    cron will automatically send an email to the crontab's owner if the program/script returns a non-zero status. So you could keep your program simple and exit with anything but 0 if there's an error (and possibly have your program print an error message to stderr, which gets included in the email from cron).
    Yes it does. Just checked.
    Thanks a lot for that. You saved some time for me.

  7. #7
    Registered User
    Join Date
    Apr 2019
    Posts
    6
    Salem,
    Thanks for your response.
    It does exectly what i was looking for.
    But i modified it a little. To condition after waitpid added WEXITSTATUS(stat).

    As soon as subject is solved it can be closed.

    Thanks a lot!
    Last edited by datswd; 04-29-2019 at 12:08 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Try Catch
    By r895 in forum C++ Programming
    Replies: 4
    Last Post: 04-23-2014, 04:23 PM
  2. What can be done with catch(...)?
    By 6tr6tr in forum C++ Programming
    Replies: 6
    Last Post: 04-17-2008, 10:27 AM
  3. Is there a catch to return in a try/catch box?
    By meili100 in forum C++ Programming
    Replies: 25
    Last Post: 11-21-2007, 01:33 PM
  4. How does one catch NaN type errors?
    By testing123 in forum C++ Programming
    Replies: 15
    Last Post: 08-16-2006, 12:42 PM
  5. Try...catch and XML
    By nickname_changed in forum C++ Programming
    Replies: 11
    Last Post: 07-07-2003, 02:02 AM

Tags for this Thread