Page 1 of 2 12 LastLast
Results 1 to 10 of 15

Thread: Cronjobs sending email - strange result when run on schedule | on-demand is alright

  1. #1
    Join Date
    Apr 2014
    Beans
    7

    Cronjobs sending email - strange result when run on schedule | on-demand is alright

    Hey all,

    I am kinda new to Cron jobs. I thought it was a nice way to automate a few things,
    Using Webmin, I created 2 cronjobs to automate tripwire & chkrootkit checks. I wanted to add more but I am running into issues that I don't understand.

    The commands I am running are:
    @daily chkrootkit | mail -s "ChkRootkit report for `uname -n`" myemail@domain.com #somecomment
    @daily tripwire --check | mail -s "Tripwirereport for `uname -n`" myemail@domain.com #some comment

    Right now I've set them both to run daily at 00:00 AM, every day(testing purposes).

    When I hit "Run now", I receive an email, with the expected report/result/output,

    When I wait for it to run as scheduled, I receive an email, without any text in the body.
    The subject is alright, but the body is empty.

    Can anyone help me out here?

    The OS is Ubuntu Server 14.04.2,
    Tools used are both CLI/Terminal and Webmin.
    Kind regards,
    Frank

  2. #2
    Join Date
    Mar 2010
    Location
    Squidbilly-Land
    Beans
    Hidden!
    Distro
    Ubuntu

    Re: Cronjobs sending email - strange result when run on schedule | on-demand is alrig

    cron doesn't get much environment, so things that your login sets up doesn't exist under cron. You have to set everything you expect in the environment, if you want it. For example, the PATH, LD_LIBRARY_PATH are minimal under cron.

    https://stackoverflow.com/questions/...-a-script-with explains how to see the environment cron provides.

    Hence, there are some best practices for shell scripting.
    * Always specify the complete, full, path to all programs you want run.
    * Never parse the output from ls, avoid using simple grep and cat - cat is almost never needed.
    * For cron entries, create a shell script to put any multi-command commands into 1 file for cron to call.
    * noclobber
    * http://www.davidpashley.com/articles...shell-scripts/

    Create ~/bin/rootkit-chk ; chmod +x ~/bin/rootkit-chk and put something like this inside:
    Code:
    /usr/bin/chkrootkit | /usr/bin/mail -s "ChkRootkit report for `/bin/hostname`" myemail\@domain.com #somecomment
    inside it. I guessed at the paths - verify for your system. Also - used hostname. Might be good to set env vars for each command (I usually use all CAPS) and might verify that the commands exist too.

    For me, if any bash/sh script gets over 2 pgs long, I'll re-write it in Perl for easier maintenance. Python would be a good choice to, but I don't "python." Both are pre-installed on Ubuntu systems while ruby may not be.

  3. #3
    Join Date
    Apr 2014
    Beans
    7

    Re: Cronjobs sending email - strange result when run on schedule | on-demand is alrig

    Thanks for your reply,
    I will test this tonight.
    1 last question though,
    The new cronjob is something like
    @daily tripwire.sh

    then the tripwire.sh has the command like you gave above?
    "tripwire --check | mail -s "Tripwirereport for `/bin/hostname`" myemail@domain.com #some comment"

    On your last note: I am a home-user, and hope not to use bash/sh scripts of those lengths.. given examples are the longest I planned to use

    Kind regards,
    Frank

  4. #4
    Join Date
    Aug 2009
    Location
    Makati City, Philippines
    Beans
    2,269
    Distro
    Xubuntu 16.04 Xenial Xerus

    Re: Cronjobs sending email - strange result when run on schedule | on-demand is alrig

    @daily tripwire.sh ---> might as well specify the shell you want this to run, such as: @daily /bin/bash tripwire.sh

    Also on your script, as suggested already, better to use full path on for the commands like TheFu said.

  5. #5
    Join Date
    Mar 2010
    Location
    Squidbilly-Land
    Beans
    Hidden!
    Distro
    Ubuntu

    Re: Cronjobs sending email - strange result when run on schedule | on-demand is alrig

    Quote Originally Posted by Frank_Barmentlo View Post
    The new cronjob is something like
    @daily tripwire.sh

    then the tripwire.sh has the command like you gave above?
    "tripwire --check | mail -s "Tripwirereport for `/bin/hostname`" myemail@domain.com #some comment"
    You need something like

    Code:
    @daily /home/userid-whatever-it-is/bin/tripwire.sh
    You cannot assume the PATH. Also, I've never used the @daily crontab - always use a crontab for a specific user - which have different parameters. I say this so you know I don't know if hte exact line is correct or not ... just that the full path to the script MUST be specified. Also - I think the email address needs the '\@' escaped, but I'm not positive. This is common for certain characters like that.

    Also - you need to specify the full path to teh tripwire program. If you don't understand, please ask. We were all new to this at some point. Things will "click" over time and your understanding will grow exponentially as the pieces fit.

    Extensions, like .sh, don't mean anything to Linux. They are not required or used by the OS at all. They are handy for humans, but don't trust them completely.
    Last edited by TheFu; May 1st, 2015 at 03:32 PM.

  6. #6
    Join Date
    Apr 2014
    Beans
    7

    Re: Cronjobs sending email - strange result when run on schedule | on-demand is alrig

    Hey TheFu,
    Thanks for your time helping me out,

    About the extensions: I am an IT-guy who grew up with windows, where extensions do matter. It's just for myself that I add extensions, so I know what kind of file I am looking at. Just a bad habbit that's stuck

    Is there any reason the full path has to be used, and is there an easy way to find the full path of binaries?
    It probably is /usr/bin or /usr/sbin, but to be sure?

    Kind regards,
    Frank

  7. #7
    Join Date
    Jan 2008
    Location
    Faversham UK
    Beans
    84

    Re: Cronjobs sending email - strange result when run on schedule | on-demand is alrig

    If you just need to know where the executable is you can run whereis or which.

    Code:
    whereis firefox
    firefox: /usr/bin/firefox /etc/firefox /usr/lib/firefox /usr/share/man/man1/firefox.1.gz
    Code:
    which firefox
    /usr/bin/firefox

  8. #8
    Join Date
    Aug 2009
    Location
    Makati City, Philippines
    Beans
    2,269
    Distro
    Xubuntu 16.04 Xenial Xerus

    Re: Cronjobs sending email - strange result when run on schedule | on-demand is alrig

    Quote Originally Posted by Frank_Barmentlo View Post

    Is there any reason the full path has to be used,
    Because a user running a command will search the default directories on the $PATH variable. This $PATH variable is different from user, root and cron. So it's important to provide the full path of the commands.

    So, if your tripwire.sh script is saved in your home folder, cron won't even search that folder. Therefore, when cron run the command "@daily tripwire.sh" it won't be able to find the script.

  9. #9
    Join Date
    Mar 2010
    Location
    Squidbilly-Land
    Beans
    Hidden!
    Distro
    Ubuntu

    Re: Cronjobs sending email - strange result when run on schedule | on-demand is alrig

    Quote Originally Posted by nerdtron View Post
    So, if your tripwire.sh script is saved in your home folder, cron won't even search that folder. Therefore, when cron run the command "@daily tripwire.sh" it won't be able to find the script.
    Actually - I'd be surprised if "$HOME" was set correctly under a cron job too. Don't assume anything. **Always** use full paths for programs, input files, output files and redirection in shell scripts. This applies even more so for cron. Learning it today will save you hours of problems in the future - I speak from experience as a formerly lazy scripter.

    Generally, PATH works the same on Linux as it does on Windows. Just Linux will only know about files marked with execute permissions that the current userid can actually run, whereas on Windows PATH knows about cmd, com, exe, dll, and a few other less-popular extensions.

    Seems you may have a slight gaps in your Linux knowledge. At that stage, finding answers by google isn't helpful. It is like jumping into the ocean when you've just learned how to doggie paddle in the shallow end.
    http://blog.jdpfu.com/2014/12/28/learning-linux provides my best answer to fill in those blanks in an ordered way. There's a great, free, no-hassle, PDF book link there to learn Linux and the command line. It isn't just what to type, rather, it is why to do something a certain way. 3 hrs of reading will prevent hours, days, weeks of frustration.

  10. #10
    Join Date
    Oct 2009
    Beans
    Hidden!
    Distro
    Ubuntu 22.04 Jammy Jellyfish

    Re: Cronjobs sending email - strange result when run on schedule | on-demand is alrig

    +1 to TheFu. I always try to use full paths in my shell scripts whether they will be run via cron or not.

    Have a read on the link in TheFu's last post as well. That should help.

    As an aside... I rarely use @daily or @weekly when setting up cronjobs. I use the normal syntax.
    https://help.ubuntu.com/community/CronHowto
    Come to #ubuntuforums! We have cookies! | Basic Ubuntu Security Guide

    Tomorrow's an illusion and yesterday's a dream, today is a solution...

Page 1 of 2 12 LastLast

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •