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

Thread: Bash script : parent / child process issue ?

  1. #1
    Join Date
    Jun 2018
    Beans
    Hidden!

    Bash script : parent / child process issue ?

    Hello there,

    I managed to write a bash script that, among other functionalities, launches another sub-process which is not another bash script but a binary (which is supposed to be run only by bash). When I launch directly my "parent" script, everything works perfectly, subprocess included. But once it starts automatically through cron, the binary subprocess doesn't work anymore.

    My crontab command was the following, in order to check and restart only if necessary my script:

    Code:
    crontab -l | { cat; echo "*/1 * * * * /bin/bash pgrep -f ~/Documents/Linux/checker > /dev/null || ~/Documents/Linux/checker"; } | crontab -
    Is there anything (or a lot of things) I've done incorrectly? Thanks in advance!
    Last edited by mohicann; June 7th, 2018 at 11:49 PM. Reason: solved

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

    Re: Bash script : parent / child process issue ?

    ~/ doesn't work in cron. Most environment variables aren't set.

  3. #3
    Join Date
    Jun 2018
    Beans
    Hidden!

    Re: Bash script : parent / child process issue ?

    Thank you for your reply. Actually, I tried to write and run some small test loop scripts with the same crontab command in order to figure out where the issue was coming from, and nonetheless it worked. But they didn't have child processes. That's why I was focusing on this possibility.

    PS: I probably got your idea. I need to check a few more things indeed, for instance the relative paths in my "parent" script.
    Last edited by mohicann; June 6th, 2018 at 07:22 PM.

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

    Re: Bash script : parent / child process issue ?

    Compare the output of
    Code:
    env > /home/mohicann/env.interactive
    to running it from a crontab
    Code:
    env > /home/mohicann/env.cron
    You'll see what is and isn't set that way.

  5. #5
    Join Date
    Jun 2018
    Beans
    Hidden!

    Re: Bash script : parent / child process issue ?

    Thank for you reply. There are exactly the same. I assume it doesn't come from relative and absolute paths?

  6. #6
    Join Date
    Feb 2009
    Location
    Dallas, TX
    Beans
    7,790
    Distro
    Ubuntu 16.04 Xenial Xerus

    Re: Bash script : parent / child process issue ?

    Hi mohicann.

    You don't need to use /bin/bash on your crontab line. This should work:
    Code:
    */1 * * * * pgrep -f ~/Documents/Linux/ch...
    ~ works depending on the shell used by the distribution. crontab uses /bin/sh. In Ubuntu /bin/sh is a link to /bin/dash. dash expands the ~ to your home directory

    Thus, in Ubuntu ~ works in crontab. However, if you want portability, use absolute paths instead.

    Hope it helps. Let us know how it goes.
    Regards.

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

    Re: Bash script : parent / child process issue ?

    Quote Originally Posted by mohicann View Post
    Thank for you reply. There are exactly the same. I assume it doesn't come from relative and absolute paths?
    I suspect you aren't doing it correct then. Let me check mine. Ok ... that ran.
    Code:
    $ wc -l  env*
      16 env.cron
      74 env.int
    The interactive env is VERY DIFFERENT from the env under cron.
    Oddly, HOME and PWD are set - those are both new to me in a crontab. Very unexpected. It wasn't that way 10 yrs ago.

    Code:
    SHELL=/bin/sh
    PATH=/usr/bin:/bin
    PWD=/home/thefu
    But a bunch of other variables are missing and the PATH is certainly tiny under cron. USER is not set, but LOGNAME is. Interesting.

    Much of my cron knowledge comes from UNIX systems, so some things are a little different.
    Last edited by TheFu; June 6th, 2018 at 08:22 PM.

  8. #8
    Join Date
    Jun 2018
    Beans
    Hidden!

    Re: Bash script : parent / child process issue ?

    Thank you for your replies and information.

    I've checked with this crontab command line:

    Code:
    crontab -l | { cat; echo "30 * * * * /home/mohicann/Documents/Linux/checker"; } | crontab -
    It starts but my subprocess still doesn't work.

    I've checked my script with bash -x and it runs perfectly, and when it is started manually, everything works fine. There really should be a trick somewhere between cron and bash or cron and my subprocess that I still don't get. Moreover, if I fill in the crontab command with my script command line (absolute path)) that starts my subprocess, it doenst' work either while it works perfectly from a bash console. It seems it can only work through my script alone or a bash console.
    Last edited by mohicann; June 7th, 2018 at 04:10 PM.

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

    Re: Bash script : parent / child process issue ?

    Simplify and test.
    What, exactly, is the line in the crontab? I know what you think it is, but what is it really?
    Which crontab, exactly, is being used?
    What are the file permissions for the script?
    Does the script depend on any interactive environment variables? I bet it does.

  10. #10
    Join Date
    Jun 2018
    Beans
    Hidden!

    Re: Bash script : parent / child process issue ?

    I've added absolute paths in my script and a chmod 777 to my subprocess (as my script was already).

    I'm using my user crontab but I've noticed something: when my script is launched manually (double click), its parent is dolphin, and when it's launched by crontab, its parent is sh. My subprocess (when it works) always has my script as parent. However, when my subprocess is directly started from a console as a main process, it has bash as parent. Indeed, there could be something set that doesn't work, such as an absolute path of bash command inside my script, that was supposed to start my subprocess. Moreover, if I start my script from a bash console, its parent is still bash but the subprocess works.

    PS: if I specify /bin/bash to run my subprocess in my script, it doesn't work either.
    Last edited by mohicann; June 7th, 2018 at 06:40 PM.

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
  •