Launching a detached process from a run phase build script.


Alex Zavatone
 

We have a mock service for our web service APIs that is generally run manually when we build our iOS apps for testing.

I’m trying to get this service which is started by rackup to run as a detached process so that the rest fo the build scripts will continue executing after it is issued.

I’ve tried nohup, adding an & after the command, disown and a few other alternatives. Can anyone help me out with a proper method to start up rackup and make it detached so that the other build scripts will continue?

I’d hate to have to resort to AppleScript.

Thanks in advance.
Alex Zavatone


Alex Zavatone
 

Just figured it out.

$SRCROOT/Build-Phases/Start_up_rack_in_test.sh > stdout 2>&1 &


Hope this helps someone else.

On Feb 4, 2019, at 3:28 PM, Alex Zavatone via Groups.Io <zav@...> wrote:

We have a mock service for our web service APIs that is generally run manually when we build our iOS apps for testing.

I’m trying to get this service which is started by rackup to run as a detached process so that the rest fo the build scripts will continue executing after it is issued.

I’ve tried nohup, adding an & after the command, disown and a few other alternatives.  Can anyone help me out with a proper method to start up rackup and make it detached so that the other build scripts will continue?

I’d hate to have to resort to AppleScript.

Thanks in advance.
Alex Zavatone




Bernie Maier
 

Alex Zavatone via Groups.Io:

Just figured it out.

$SRCROOT/Build-Phases/Start_up_rack_in_test.sh > stdout 2>&1 &

Hope this helps someone else.
Although you have figured it out, I'd recommend to anyone who can spare the engineering time to move this kind of thing out of the Xcode part of the build. Something like Fastlane would be a good candidate as the infrastructure to build parts of this: you'd define a Fastlane lanes to kick off the build, start up and tear down your mock service, a lane to run your automated tests and then a master lane to coordinate all this.

Still, that's a useful snippet to know, so thanks for mentioning it. I'm just suggesting an alternative approach, not trying to tell you how to do your job.

Cheers!

On Feb 4, 2019, at 3:28 PM, Alex Zavatone via Groups.Io
<zav=mac.com@groups.io> wrote:

We have a mock service for our web service APIs that is generally run
manually when we build our iOS apps for testing.

I’m trying to get this service which is started by rackup to run as a
detached process so that the rest fo the build scripts will continue
executing after it is issued.

I’ve tried nohup, adding an & after the command, disown and a few other
alternatives. Can anyone help me out with a proper method to start up
rackup and make it detached so that the other build scripts will continue?

I’d hate to have to resort to AppleScript.

Thanks in advance.
Alex Zavatone



Alex Zavatone
 

The thing I would like to know most is why it works. While using stdout yesterday allowed the output to appear in the build log, this morning it is missing.

Also, if the first > and argument are removed, the build script ignores the trailing & and waits for the script to finish.

I’d actually like to know why this works and get it to work without having to redirect output to a file.

On Feb 5, 2019, at 4:26 AM, Bernie Maier <apple-dev@worklists.blurk.net> wrote:

Alex Zavatone via Groups.Io:

Just figured it out.

$SRCROOT/Build-Phases/Start_up_rack_in_test.sh > stdout 2>&1 &

Hope this helps someone else.
Although you have figured it out, I'd recommend to anyone who can spare the engineering time to move this kind of thing out of the Xcode part of the build. Something like Fastlane would be a good candidate as the infrastructure to build parts of this: you'd define a Fastlane lanes to kick off the build, start up and tear down your mock service, a lane to run your automated tests and then a master lane to coordinate all this.

Still, that's a useful snippet to know, so thanks for mentioning it. I'm just suggesting an alternative approach, not trying to tell you how to do your job.

Cheers!

On Feb 4, 2019, at 3:28 PM, Alex Zavatone via Groups.Io
<zav=mac.com@groups.io> wrote:

We have a mock service for our web service APIs that is generally run
manually when we build our iOS apps for testing.

I’m trying to get this service which is started by rackup to run as a
detached process so that the rest fo the build scripts will continue
executing after it is issued.

I’ve tried nohup, adding an & after the command, disown and a few other
alternatives. Can anyone help me out with a proper method to start up
rackup and make it detached so that the other build scripts will continue?

I’d hate to have to resort to AppleScript.

Thanks in advance.
Alex Zavatone








Jonathan Prescott
 

Do you have a file in $SRCROOT called “stdout?”  That should have all the logging info from running the script.  Have you tried just “$SRCROOT//Build-Phases/Start_up_rack_in_test.sh &” (no I/O re-direction)?  With no I/O re-direction, stdout and stderr are automatically linked to the terminal, or, in the case of an Xcode run, the Console (which is the terminal).  Not sure what your script is doing that could mess things up, but, running a test script using these variations causes these results (all running in a shell environment with sh running the script “testit.sh”

sh testit.sh —> Script works like it should in foreground
sh testit.sh & —> Script runs in a background process, prints to the terminal screen
sh testit.sh >stdout 2>&1  —> Script runs in foreground, prints stdout and stderr to file “stdout” (2>&1 re-directs stderr to file channel connected to stdout - the &1)
sh testit.sh >stdout 2>&1 & —> Script runs in background, prints everything to file “stdout”
sh testit.sh 2>&1 & —> Script runs in background, but, immediately exits with an error (pipe error, I think, 2>&1 assumes you are re-directing stdout to something)

When the script failed, did the Xcode detailed error log show you anything about your script’s error status?

Jonathan

---------------------------------------------------------
Jonathan Prescott



On Feb 5, 2019, at 11:30 AM, Alex Zavatone via Groups.Io <zav@...> wrote:

The thing I would like to know most is why it works.  While using stdout yesterday allowed the output to appear in the build log, this morning it is missing.

Also, if the first > and argument are removed, the build script ignores the trailing & and waits for the script to finish.

I’d actually like to know why this works and get it to work without having to redirect output to a file.
On Feb 5, 2019, at 4:26 AM, Bernie Maier <apple-dev@...> wrote:

Alex Zavatone via Groups.Io:

Just figured it out.

$SRCROOT/Build-Phases/Start_up_rack_in_test.sh > stdout 2>&1 &

Hope this helps someone else.

Although you have figured it out, I'd recommend to anyone who can spare the engineering time to move this kind of thing out of the Xcode part of the build. Something like Fastlane would be a good candidate as the infrastructure to build parts of this: you'd define a Fastlane lanes to kick off the build, start up and tear down your mock service, a lane to run your automated tests and then a master lane to coordinate all this.

Still, that's a useful snippet to know, so thanks for mentioning it. I'm just suggesting an alternative approach, not trying to tell you how to do your job.

Cheers!

On Feb 4, 2019, at 3:28 PM, Alex Zavatone via Groups.Io
<zav@...> wrote:

We have a mock service for our web service APIs that is generally run
manually when we build our iOS apps for testing.

I’m trying to get this service which is started by rackup to run as a
detached process so that the rest fo the build scripts will continue
executing after it is issued.

I’ve tried nohup, adding an & after the command, disown and a few other
alternatives.  Can anyone help me out with a proper method to start up
rackup and make it detached so that the other build scripts will continue?

I’d hate to have to resort to AppleScript.

Thanks in advance.
Alex Zavatone


















Jonathan Prescott
 

Not sure how you are running your script.  I put together a small Xcode project that builds a simple “Hello, World” application, and an associated Script that could be run in a variety of ways, foreground, background.  The script writes out a welcome string to stdout, writes all the process ids, sleeps for 10 seconds, then writes out a close-out string to stdout and exits.

I’m assuming you are running the script as a run/test scheme pre-action.  Pre-action script I/O is not captured in the build logs since they are run prior to Xcode setting up the Console I/O pipes. Any I/O made in the pre-action script goes to the process pipes setup up by the process that invokes Xcode.  Since you probably run Xcode at times by double-clicking it, the process invoking Xcode, launchd, has its stdout and stderr connected to /dev/null, which throws everything into the bitbucket.  I had to add an “exec >file 2>&1” in the run pre-action script in order to capture the output from my script.  Haven’t tried it, but if you run an appropriate xcodebuild at the command line, you might get the logging output at the terminal.

The only way I was able to get the output of the script to show up was as a build phase run script, but, I’m not sure that’s what you want to happen, since build phases are run serially, and it would have to wait until the background process completed to keep going.

Does your script put the rackup daemon in the background?  Have you looked at running the framework as a launchd user-agent?

Jonathan

---------------------------------------------------------
Jonathan Prescott



On Feb 5, 2019, at 12:05 PM, Jonathan Prescott via Groups.Io <jprescott12@...> wrote:

Do you have a file in $SRCROOT called “stdout?”  That should have all the logging info from running the script.  Have you tried just “$SRCROOT//Build-Phases/Start_up_rack_in_test.sh &” (no I/O re-direction)?  With no I/O re-direction, stdout and stderr are automatically linked to the terminal, or, in the case of an Xcode run, the Console (which is the terminal).  Not sure what your script is doing that could mess things up, but, running a test script using these variations causes these results (all running in a shell environment with sh running the script “testit.sh”

sh testit.sh —> Script works like it should in foreground
sh testit.sh & —> Script runs in a background process, prints to the terminal screen
sh testit.sh >stdout 2>&1  —> Script runs in foreground, prints stdout and stderr to file “stdout” (2>&1 re-directs stderr to file channel connected to stdout - the &1)
sh testit.sh >stdout 2>&1 & —> Script runs in background, prints everything to file “stdout”
sh testit.sh 2>&1 & —> Script runs in background, but, immediately exits with an error (pipe error, I think, 2>&1 assumes you are re-directing stdout to something)

When the script failed, did the Xcode detailed error log show you anything about your script’s error status?

Jonathan

---------------------------------------------------------
Jonathan Prescott



On Feb 5, 2019, at 11:30 AM, Alex Zavatone via Groups.Io <zav@...> wrote:

The thing I would like to know most is why it works.  While using stdout yesterday allowed the output to appear in the build log, this morning it is missing.

Also, if the first > and argument are removed, the build script ignores the trailing & and waits for the script to finish.

I’d actually like to know why this works and get it to work without having to redirect output to a file.
On Feb 5, 2019, at 4:26 AM, Bernie Maier <apple-dev@...> wrote:

Alex Zavatone via Groups.Io:

Just figured it out.

$SRCROOT/Build-Phases/Start_up_rack_in_test.sh > stdout 2>&1 &

Hope this helps someone else.

Although you have figured it out, I'd recommend to anyone who can spare the engineering time to move this kind of thing out of the Xcode part of the build. Something like Fastlane would be a good candidate as the infrastructure to build parts of this: you'd define a Fastlane lanes to kick off the build, start up and tear down your mock service, a lane to run your automated tests and then a master lane to coordinate all this.

Still, that's a useful snippet to know, so thanks for mentioning it. I'm just suggesting an alternative approach, not trying to tell you how to do your job.

Cheers!

On Feb 4, 2019, at 3:28 PM, Alex Zavatone via Groups.Io
<zav@...> wrote:

We have a mock service for our web service APIs that is generally run
manually when we build our iOS apps for testing.

I’m trying to get this service which is started by rackup to run as a
detached process so that the rest fo the build scripts will continue
executing after it is issued.

I’ve tried nohup, adding an & after the command, disown and a few other
alternatives.  Can anyone help me out with a proper method to start up
rackup and make it detached so that the other build scripts will continue?

I’d hate to have to resort to AppleScript.

Thanks in advance.
Alex Zavatone



















Alex Zavatone
 

Heh, yeah.  I do now.  I changed it to ~/rack_log.text

The odd thing was that sometimes, I would get output to the build log window, which was ideal.  I’d rather not redirect output to another file.  

Since It looks like the easiest solution is to use two files.  One that is launched as part of the Build Phase and in turn it makes a call to open another shell script.  This shell script has the call to launch rackup with > ~/rack.log 2>&1 & after it.  Hacks, butThis appears to work and preserves almost all of the output in the build console.




On Feb 5, 2019, at 11:05 AM, Jonathan Prescott via Groups.Io <jprescott12@...> wrote:

Do you have a file in $SRCROOT called “stdout?”  That should have all the logging info from running the script.  Have you tried just “$SRCROOT//Build-Phases/Start_up_rack_in_test.sh &” (no I/O re-direction)?  With no I/O re-direction, stdout and stderr are automatically linked to the terminal, or, in the case of an Xcode run, the Console (which is the terminal).

Yes.  It runs a rack up server and keeps it open in a Terminal session.

If I did that in the build phase, the build phase never completes.

 Not sure what your script is doing that could mess things up, but, running a test script using these variations causes these results (all running in a shell environment with sh running the script “testit.sh”

sh testit.sh —> Script works like it should in foreground
sh testit.sh & —> Script runs in a background process, prints to the terminal screen
sh testit.sh >stdout 2>&1  —> Script runs in foreground, prints stdout and stderr to file “stdout” (2>&1 re-directs stderr to file channel connected to stdout - the &1)
sh testit.sh >stdout 2>&1 & —> Script runs in background, prints everything to file “stdout”
sh testit.sh 2>&1 & —> Script runs in background, but, immediately exits with an error (pipe error, I think, 2>&1 assumes you are re-directing stdout to something)

When the script failed, did the Xcode detailed error log show you anything about your script’s error status?


By “failing”, I mean failing to detach the process so that the rest of the build could continue.

Here is how I found out how to do this.


Thank you.


Jonathan

---------------------------------------------------------
Jonathan Prescott



On Feb 5, 2019, at 11:30 AM, Alex Zavatone via Groups.Io <zav@...> wrote:

The thing I would like to know most is why it works.  While using stdout yesterday allowed the output to appear in the build log, this morning it is missing.

Also, if the first > and argument are removed, the build script ignores the trailing & and waits for the script to finish.

I’d actually like to know why this works and get it to work without having to redirect output to a file.
On Feb 5, 2019, at 4:26 AM, Bernie Maier <apple-dev@...> wrote:

Alex Zavatone via Groups.Io:

Just figured it out.

$SRCROOT/Build-Phases/Start_up_rack_in_test.sh > stdout 2>&1 &

Hope this helps someone else.

Although you have figured it out, I'd recommend to anyone who can spare the engineering time to move this kind of thing out of the Xcode part of the build. Something like Fastlane would be a good candidate as the infrastructure to build parts of this: you'd define a Fastlane lanes to kick off the build, start up and tear down your mock service, a lane to run your automated tests and then a master lane to coordinate all this.

Still, that's a useful snippet to know, so thanks for mentioning it. I'm just suggesting an alternative approach, not trying to tell you how to do your job.

Cheers!

On Feb 4, 2019, at 3:28 PM, Alex Zavatone via Groups.Io
<zav@...> wrote:

We have a mock service for our web service APIs that is generally run
manually when we build our iOS apps for testing.

I’m trying to get this service which is started by rackup to run as a
detached process so that the rest fo the build scripts will continue
executing after it is issued.

I’ve tried nohup, adding an & after the command, disown and a few other
alternatives.  Can anyone help me out with a proper method to start up
rackup and make it detached so that the other build scripts will continue?

I’d hate to have to resort to AppleScript.

Thanks in advance.
Alex Zavatone


















Jonathan Prescott
 

Thanks for the pointer to StackOverflow question
Jonathan

---------------------------------------------------------
Jonathan Prescott



On Feb 5, 2019, at 4:09 PM, Alex Zavatone via Groups.Io <zav@...> wrote:

Heh, yeah.  I do now.  I changed it to ~/rack_log.text

The odd thing was that sometimes, I would get output to the build log window, which was ideal.  I’d rather not redirect output to another file.  

Since It looks like the easiest solution is to use two files.  One that is launched as part of the Build Phase and in turn it makes a call to open another shell script.  This shell script has the call to launch rackup with > ~/rack.log 2>&1 & after it.  Hacks, butThis appears to work and preserves almost all of the output in the build console.




On Feb 5, 2019, at 11:05 AM, Jonathan Prescott via Groups.Io <jprescott12@...> wrote:

Do you have a file in $SRCROOT called “stdout?”  That should have all the logging info from running the script.  Have you tried just “$SRCROOT//Build-Phases/Start_up_rack_in_test.sh &” (no I/O re-direction)?  With no I/O re-direction, stdout and stderr are automatically linked to the terminal, or, in the case of an Xcode run, the Console (which is the terminal). 

Yes.  It runs a rack up server and keeps it open in a Terminal session.

If I did that in the build phase, the build phase never completes.

 Not sure what your script is doing that could mess things up, but, running a test script using these variations causes these results (all running in a shell environment with sh running the script “testit.sh”

sh testit.sh —> Script works like it should in foreground
sh testit.sh & —> Script runs in a background process, prints to the terminal screen
sh testit.sh >stdout 2>&1  —> Script runs in foreground, prints stdout and stderr to file “stdout” (2>&1 re-directs stderr to file channel connected to stdout - the &1)
sh testit.sh >stdout 2>&1 & —> Script runs in background, prints everything to file “stdout”
sh testit.sh 2>&1 & —> Script runs in background, but, immediately exits with an error (pipe error, I think, 2>&1 assumes you are re-directing stdout to something)

When the script failed, did the Xcode detailed error log show you anything about your script’s error status?


By “failing”, I mean failing to detach the process so that the rest of the build could continue.

Here is how I found out how to do this.


Thank you.


Jonathan

---------------------------------------------------------
Jonathan Prescott



On Feb 5, 2019, at 11:30 AM, Alex Zavatone via Groups.Io <zav@...> wrote:

The thing I would like to know most is why it works.  While using stdout yesterday allowed the output to appear in the build log, this morning it is missing.

Also, if the first > and argument are removed, the build script ignores the trailing & and waits for the script to finish.

I’d actually like to know why this works and get it to work without having to redirect output to a file.
On Feb 5, 2019, at 4:26 AM, Bernie Maier <apple-dev@...> wrote:

Alex Zavatone via Groups.Io:

Just figured it out.

$SRCROOT/Build-Phases/Start_up_rack_in_test.sh > stdout 2>&1 &

Hope this helps someone else.

Although you have figured it out, I'd recommend to anyone who can spare the engineering time to move this kind of thing out of the Xcode part of the build. Something like Fastlane would be a good candidate as the infrastructure to build parts of this: you'd define a Fastlane lanes to kick off the build, start up and tear down your mock service, a lane to run your automated tests and then a master lane to coordinate all this.

Still, that's a useful snippet to know, so thanks for mentioning it. I'm just suggesting an alternative approach, not trying to tell you how to do your job.

Cheers!

On Feb 4, 2019, at 3:28 PM, Alex Zavatone via Groups.Io
<zav@...> wrote:

We have a mock service for our web service APIs that is generally run
manually when we build our iOS apps for testing.

I’m trying to get this service which is started by rackup to run as a
detached process so that the rest fo the build scripts will continue
executing after it is issued.

I’ve tried nohup, adding an & after the command, disown and a few other
alternatives.  Can anyone help me out with a proper method to start up
rackup and make it detached so that the other build scripts will continue?

I’d hate to have to resort to AppleScript.

Thanks in advance.
Alex Zavatone



















Alex Zavatone
 

Just in a build phase run phase.

The goal is to test if we are running from a test build config and if so, restart our mock web server which is comprised of rackup and sinatra.

I have a folder of build scripts called Build-Scripts which can be added to specific targets as needed.  


On Feb 5, 2019, at 1:01 PM, Jonathan Prescott via Groups.Io <jprescott12@...> wrote:

Not sure how you are running your script.  I put together a small Xcode project that builds a simple “Hello, World” application, and an associated Script that could be run in a variety of ways, foreground, background.  The script writes out a welcome string to stdout, writes all the process ids, sleeps for 10 seconds, then writes out a close-out string to stdout and exits.

I’m assuming you are running the script as a run/test scheme pre-action.  Pre-action script I/O is not captured in the build logs since they are run prior to Xcode setting up the Console I/O pipes. Any I/O made in the pre-action script goes to the process pipes setup up by the process that invokes Xcode.  Since you probably run Xcode at times by double-clicking it, the process invoking Xcode, launchd, has its stdout and stderr connected to /dev/null, which throws everything into the bitbucket.  I had to add an “exec >file 2>&1” in the run pre-action script in order to capture the output from my script.  Haven’t tried it, but if you run an appropriate xcodebuild at the command line, you might get the logging output at the terminal.

The only way I was able to get the output of the script to show up was as a build phase run script, but, I’m not sure that’s what you want to happen, since build phases are run serially, and it would have to wait until the background process completed to keep going.

Does your script put the rackup daemon in the background?  Have you looked at running the framework as a launchd user-agent?

Jonathan

---------------------------------------------------------
Jonathan Prescott



On Feb 5, 2019, at 12:05 PM, Jonathan Prescott via Groups.Io <jprescott12@...> wrote:

Do you have a file in $SRCROOT called “stdout?”  That should have all the logging info from running the script.  Have you tried just “$SRCROOT//Build-Phases/Start_up_rack_in_test.sh &” (no I/O re-direction)?  With no I/O re-direction, stdout and stderr are automatically linked to the terminal, or, in the case of an Xcode run, the Console (which is the terminal).  Not sure what your script is doing that could mess things up, but, running a test script using these variations causes these results (all running in a shell environment with sh running the script “testit.sh”

sh testit.sh —> Script works like it should in foreground
sh testit.sh & —> Script runs in a background process, prints to the terminal screen
sh testit.sh >stdout 2>&1  —> Script runs in foreground, prints stdout and stderr to file “stdout” (2>&1 re-directs stderr to file channel connected to stdout - the &1)
sh testit.sh >stdout 2>&1 & —> Script runs in background, prints everything to file “stdout”
sh testit.sh 2>&1 & —> Script runs in background, but, immediately exits with an error (pipe error, I think, 2>&1 assumes you are re-directing stdout to something)

When the script failed, did the Xcode detailed error log show you anything about your script’s error status?

Jonathan

---------------------------------------------------------
Jonathan Prescott



On Feb 5, 2019, at 11:30 AM, Alex Zavatone via Groups.Io <zav@...> wrote:

The thing I would like to know most is why it works.  While using stdout yesterday allowed the output to appear in the build log, this morning it is missing.

Also, if the first > and argument are removed, the build script ignores the trailing & and waits for the script to finish.

I’d actually like to know why this works and get it to work without having to redirect output to a file.
On Feb 5, 2019, at 4:26 AM, Bernie Maier <apple-dev@...> wrote:

Alex Zavatone via Groups.Io:

Just figured it out.

$SRCROOT/Build-Phases/Start_up_rack_in_test.sh > stdout 2>&1 &

Hope this helps someone else.

Although you have figured it out, I'd recommend to anyone who can spare the engineering time to move this kind of thing out of the Xcode part of the build. Something like Fastlane would be a good candidate as the infrastructure to build parts of this: you'd define a Fastlane lanes to kick off the build, start up and tear down your mock service, a lane to run your automated tests and then a master lane to coordinate all this.

Still, that's a useful snippet to know, so thanks for mentioning it. I'm just suggesting an alternative approach, not trying to tell you how to do your job.

Cheers!

On Feb 4, 2019, at 3:28 PM, Alex Zavatone via Groups.Io
<zav@...> wrote:

We have a mock service for our web service APIs that is generally run
manually when we build our iOS apps for testing.

I’m trying to get this service which is started by rackup to run as a
detached process so that the rest fo the build scripts will continue
executing after it is issued.

I’ve tried nohup, adding an & after the command, disown and a few other
alternatives.  Can anyone help me out with a proper method to start up
rackup and make it detached so that the other build scripts will continue?

I’d hate to have to resort to AppleScript.

Thanks in advance.
Alex Zavatone