phpdesktop icon indicating copy to clipboard operation
phpdesktop copied to clipboard

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

Open cztomczak opened this issue 8 years ago • 7 comments

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 avatar Mar 16 '16 13:03 cztomczak

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 avatar Mar 28 '18 11:03 cztomczak

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

cztomczak avatar Mar 28 '18 11:03 cztomczak

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 <[email protected]>
 */

// ...

/*
|--------------------------------------------------------------------------
| 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 avatar Feb 08 '20 04:02 ctf0

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

ctf0 avatar Feb 08 '20 13:02 ctf0

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

Hanson avatar Feb 27 '20 02:02 Hanson

@Hanson sadly no 😢

ctf0 avatar Feb 27 '20 03:02 ctf0

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

darkterminal avatar Dec 25 '21 06:12 darkterminal