Results 1 to 4 of 4

Thread: bash : $_ in cronjob is empty

  1. #1
    Join Date
    Jul 2012
    Beans
    2

    Question bash : $_ in cronjob is empty

    cd some_dir && php some_crone.php || echo "$? $_" | xargs ./some_handler.php
    So when I run this code from console I got a correct value for $? and for $_

    $? - exit status $_ - Gives the last argument to the previous command. At the shell startup, it gives the absolute filename of the shell script being executed.

    But when I put this code to cronjob :

    crontab -e
    */1 * * * * cd some_dir && php some_crone.php || echo "$? $_" | xargs ./some_handler.php
    I got an empty $_. Please help. I can not understand what a problem?

  2. #2
    Join Date
    Sep 2006
    Beans
    8,627
    Distro
    Ubuntu 14.04 Trusty Tahr

    wrap it in a script

    You might want to wrap cd some_dir && php some_crone.php || echo "$? $_" | xargs ./some_handler.php inside a script and then call the script from cron. I sometimes find that is necessary with complex actions.

  3. #3
    Join Date
    Aug 2010
    Location
    Lancs, United Kingdom
    Beans
    1,588
    Distro
    Ubuntu Mate 16.04 Xenial Xerus

    Re: bash : $_ in cronjob is empty

    Quote Originally Posted by ashalaenko View Post
    But when I put this code to cronjob :

    crontab -e
    */1 * * * * cd some_dir && php some_crone.php || echo "$? $_" | xargs ./some_handler.php
    I got an empty $_. Please help. I can not understand what a problem?
    Change crontab's default shell from /bin/sh to /bin/bash by adding the following line to your crontab.
    Code:
    SHELL=/bin/bash

  4. #4
    Join Date
    Jul 2012
    Beans
    2

    Cool Re: bash : $_ in cronjob is empty

    Thanks. Created bash script script and it works well. So about
    SHELL=/bin/bash - I could not change cron variables - had not permissions

Tags for this Thread

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
  •