command line - Halt PHP CLI script to give the user possibility to exit with CTRL-C -
how pause execution of command line script give user possibility exit ctrl+c?
for example have script deleting user id number of tables, halt script before doing it:
<?php define('dbhost', '/tmp'); define('dbname', 'xxx'); define('dbuser', 'yyy'); define('dbpass', 'zzz'); $tables = array('pref_game', 'pref_hand', 'pref_luck', 'pref_match', 'pref_misere', 'pref_money', 'pref_pass', 'pref_rep', 'pref_status', 'pref_users' ); if ($argc != 2) exit("missing user id\n"); if (!preg_match('/^([a-z]{2}[0-9]+)$/', $argv[1], $matches)) exit("invalid user id\n"); $id = $matches[1]; printf("deleting user %s press ctrl-c abort\n\n", $id); # xxx stop here, how? xxx try { $db = new pdo('pgsql:host=' . dbhost . '; dbname=' . dbname, dbuser, dbpass); $db->setattribute(pdo::attr_errmode, pdo::errmode_exception); foreach ($tables $table) { $sql = sprintf('delete %s id=?', $table); run_sql($db, $sql, $id); } } catch (exception $e) { exit('database problem: ' . $e->getmessage()); } function run_sql($db, $sql, $arg) { $sth = $db->prepare($sql); $sth->execute(array($arg)); printf("sql: %s\naffected rows: %d\n\n", $sql, $sth->rowcount()); } ?>
using centos linux 5.5 + php 5.1.6 + postgresql 8.4.7.
try with:
printf("deleting user %s press ctrl-c abort, enter continue\n\n", $id); $fp = fopen("php://stdin","r"); fgets($fp); echo "ok!";
Comments
Post a Comment