Skip to content

An option in settings.json to specify a path to a script or exe that will be executed when application shuts down #186

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
cztomczak opened this issue Mar 16, 2016 · 7 comments

Comments

@cztomczak
Copy link
Owner

Script could be .bat or a .php file. In case of php use the existing php interpreter specified in settings.json. This option could be named "execute_on_shutdown".

See also a similar issue #163 for a new option named "execute_on_startup".

@cztomczak
Copy link
Owner Author

cztomczak commented Mar 28, 2018

This can be accomplished using "NirCmd" utility:
http://www.nirsoft.net/utils/nircmd.html

This tool has "exec" and "waitprocess" commands with which you can accomplish this task, see:
http://nircmd.nirsoft.net/waitprocess.html

For example, something like this should work:

Step 1: execute the shutdown.php script in background when your application starts (e.g. in index.php only when session starts)

exec('"C:/path/to/nircmd.exe" exec "C:/path/to/phpdesktop/php/php.exe" "C:/path/to/phpdesktop/www/shutdown.php"');

Step 2: the shutdown.php script will wait for phpdesktop.exe process until it terminates

set_time_limit(0);
exec('"C:/path/to/nircmd.exe" waitprocess "C:/path/to/phpdesktop/phpdesktop.exe");
# Now execute PHP code that should run when app terminates
....

@cztomczak
Copy link
Owner Author

If someone would like to create an example for phpdesktop with the code in post above then pull requests are welcome.

@ctf0
Copy link

ctf0 commented Feb 8, 2020

hi @cztomczak i've made a laravel example based on the above to connect/disconnect mariadb on app start & shutdown but i have a couple of issues

  • first: the index.php
<?php


/**
 * Laravel - A PHP Framework For Web Artisans
 *
 * @package  Laravel
 * @author   Taylor Otwell <taylor@laravel.com>
 */

// ...

/*
|--------------------------------------------------------------------------
| phpdesktop auto start/shutdown db
|--------------------------------------------------------------------------
*/

// start
require_once(__DIR__.'/db-start.php');

// shutdown
require_once(__DIR__.'/db-stop.php');

/*
|--------------------------------------------------------------------------
| Run The Application
|--------------------------------------------------------------------------
|
| Once we have the application, we can handle the incoming request
| through the kernel, and send the associated response back to
| the client's browser allowing them to enjoy the creative
| and wonderful application we have prepared for them.
|
*/

// ...
  • next db-start.php
<?php

$nircmd = 'C:/Users/loko/Desktop/phpdesktop/_pkgs/nircmd/nircmd.exe';
$mariadb_start = 'C:/Users/loko/Desktop/phpdesktop/_pkgs/db/bin/mysqld.exe --console --skip-grant-tables';

exec("$nircmd exec hide $mariadb_start");
  • and db-stop.php
<?php

<?php

set_time_limit(0);

// wait for dt to close

$nircmd = 'C:/Users/loko/Desktop/phpdesktop/_pkgs/nircmd/nircmd.exe';
$desktop = 'C:/Users/loko/Desktop/phpdesktop/phpdesktop.exe';

exec("$nircmd waitprocess $desktop");

// now execute any code that should run when app terminates

$mariadb_stop = 'C:/Users/loko/Desktop/phpdesktop/_pkgs/db/bin/mysqladmin.exe shutdown';
exec("$nircmd exec hide $mariadb_stop");

the issues i have

  1. on start, db starts, but the app shows a white blank page, inspecting the body DOM shows its empty
  2. the shutdown cmnd never run

not sure if am missing something, so any help is appreciated

@ctf0
Copy link

ctf0 commented Feb 8, 2020

update

i think i've found the reason, what happens is that any process that runs through phpdesktop in our case running the db is considered a child of that process,
therefor even if we closed phpdesktop the waitprocess never runs because the db child process is still running,
so only if we closed that one, the waitprocess cmnd will then run

@Hanson
Copy link

Hanson commented Feb 27, 2020

@ctf0 hi, have you fix it? Can you provider a example ?

@ctf0
Copy link

ctf0 commented Feb 27, 2020

@Hanson sadly no 😢

@darkterminal
Copy link

I have pull example how to create Windows Print Service on start up and on shutdown application just feel free to take a look #305 maybe it's help you to know how it's work for me.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants