Skip to main content
Solved

Call Script on Script Exit


Forum|alt.badge.img+9

I'm trying to figure out how I can call a script as one exits. I do not want to run the script inside of the other script. I want it to call the next one as it exits. I'm not sure if this can be done. Here is a better representation of it.

First Script runs
Exits - Calls Second Script
Second Script runs
exits - Calls Third
etc
etc
etc

Best answer by GaToRAiD

Ouch, just figured it out, I will load a launch daemon just before exiting. This daemon will call the script.

View original
Did this topic help you find an answer to your question?

6 replies

Forum|alt.badge.img+9
  • Author
  • Contributor
  • 116 replies
  • Answer
  • July 17, 2014

Ouch, just figured it out, I will load a launch daemon just before exiting. This daemon will call the script.


BrysonTyrrell
Forum|alt.badge.img+19
  • Valued Contributor
  • 85 replies
  • July 18, 2014

You could also use "trap" in each script which is a shell builtin.

#!/bin/bash

# Define a function to call for the trap
OnExit() {
    run script
    # or execute policy
}

# Tell the script to call the function when an exit signal occurs
trap OnExit exit

# Do the script's stuff
do a command
do a command

# Now on exit it will always trigger the above function
exit 0

I have more fleshed out examples in a post I did here:
http://bryson3gps.wordpress.com/2013/11/10/bash-favorites/


talkingmoose
Forum|alt.badge.img+36
  • Community Manager
  • 1900 replies
  • July 19, 2014

Adding an ampersand at the end of a line in a script will run that process in the background and should allow your new forked process to continue although your script is exited.

#!/bin/sh
    run stuff in your script
    sh /path/to/another/script.sh &
exit 0

A downside to this method is you can't easily return any results to the JSS from the forked process.


Forum|alt.badge.img+17
  • Contributor
  • 881 replies
  • April 16, 2015

Did something change with Bash or the way the JSS runs scripts? I cannot for the life of me get a thread to fork or a trap to execute on exit without Casper waiting for it to complete while I am trying to schedule a restart, which means the policy log never gets uploaded to the JSS (the restart interrupts it). My tests work fine outside of Casper.

I am thinking a launchdaemon is my only option. I would use a policy-driven restart, except the restart occurs even if the policy fails, which is very bad for my needs.


Forum|alt.badge.img+18
  • Valued Contributor
  • 1007 replies
  • April 16, 2015

I moved a lot of postinstall stuff into running out of launchdaemon just for that express reason that the casper policy finishes successfully, and what I need to happen at the back end finishes. I usually have my recon built in the postinstall if I need the database updated as well.


Forum|alt.badge.img+17
  • Contributor
  • 881 replies
  • April 16, 2015

Yeah, I just built a launchdaemon to run a script for my reboot, and that solved it.


Reply


Cookie policy

We use cookies to enhance and personalize your experience. If you accept you agree to our full cookie policy. Learn more about our cookies.

 
Cookie settings