Score:1

Cron executes while partly ignoring trigger

so flag

There's a problem with my crontab

I set

*/5 * * * * /home/money/relaunch.sh

in crontab -e

in relaunch.sh is

#!/bin/bash
/usr/bin/pgrep -l -x xyz || /home/money/a.sh && /home/money/xyz.sh

However, when applying this crontab it launches new instance of xyz.sh every 5 minutes regardless of

/usr/bin/pgrep -l -x xyz ||

How do I get this right?

Jaromanda X avatar
ru flag
do you know that `-x` is exact match?
God of Money avatar
so flag
of course, the program itself is named xyz in this case
Jaromanda X avatar
ru flag
so `/home/money/xyz.sh` runs a program called `xyz` - it's not clear from what you posted that would be the case - because, of course, `xyz` is not exactly `xyz.sh`
God of Money avatar
so flag
sorry i did not make it clear. any way to solve this?
Jaromanda X avatar
ru flag
so, to confirm, there IS a process called exactly `xyz` started by `xyz.sh`?
God of Money avatar
so flag
yes u are right.
Score:2
eg flag

In your case, you have 2 sequential conditional operators.

script1 || script2 && script3

The key is to think of the problem backward. Script3 will run when the exit code it receives is 0. Let's analyze the two possible outcomes of running script1.

  • Case1. Script1 is successful - it produces exit code 0; script2 is never run; script3 receives the exit code 0 from script1 and runs.
  • Case2. Script1 is unsuccessful - it produces exit code 1 (or other non-zero code); script2 is run and exits with code 0; script2 runs again.

I don't know what you are trying to do, but I can see two scenarios depending on when you want script3 to run.

  1. You want to run script3 only if script2 is successful. Then you should use a subshell:

     script1 || (script2 && script3)
    
  2. You want to run script2 if script1 fails and script3 if script1 is successful. Then you just change the places of 2 and 3:

     script1 && script3 || script2
    
God of Money avatar
so flag
I want to run script2 following by script3 in case that script1 fails
Smoke avatar
eg flag
Then the first solution should work for you: "script1 || (script2 && script3)"
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.