In general, the situation is this. There is a script that can work in some rare cases longer than the interval in a cronjob, but you need to run it every minute, since in 95% of cases it works in a few seconds. It is necessary to exclude the possibility of re-launch. Immediately make a reservation, this is free hosting, so there is no SSH access, and it is impossible to monitor processes. It remains only to implement this using PHP

Tried so

//БтатичСский Ρ„Π°ΠΉΠ» ΠΊΠΎΡ‚ΠΎΡ€Ρ‹ΠΉ Π±ΡƒΠ΄Π΅Ρ‚ Ρ…Ρ€Π°Π½ΠΈΡ‚ΡŒ имя Π²Ρ€Π΅ΠΌΠ΅Π½Π½ΠΎΠ³ΠΎ Ρ„Π°ΠΉΠ»Π° $lockFilename = __DIR__ . '/lock.dat'; //ΠŸΡ€ΠΎΠ²Π΅Ρ€ΡΠ΅ΠΌ сущСствуСт Π»ΠΈ Π²Ρ€Π΅ΠΌΠ΅Π½Π½Ρ‹ΠΉ Ρ„Π°ΠΉΠ» if (file_exists($lockFilename) && ($lock = file_get_contents($lockFilename)) && file_exists($lock)) { echo "Cron running now {$lock}\n"; exit(); } //Π€Π°ΠΉΠ» Π΄ΠΎΠ»ΠΆΠ΅Π½ ΡƒΠΌΠ΅Ρ€Π΅Ρ‚ΡŒ ΠΏΡ€ΠΈ Π·Π°ΠΊΡ€Ρ‹Ρ‚ΠΈΠΈ дСскриптора $tmpHandle = tmpfile(); //ΠŸΠΎΠ»ΡƒΡ‡Π°Π΅ΠΌ имя Π²Ρ€Π΅ΠΌΠ΅Π½Π½ΠΎΠ³ΠΎ Ρ„Π°ΠΉΠ»Π° $metaData = stream_get_meta_data($tmpHandle); //ЗаписываСм имя Π²Ρ€Π΅ΠΌΠ΅Π½Π½ΠΎΠ³ΠΎ Ρ„Π°ΠΉΠ»Π° Π² статичСский Ρ„Π°ΠΉΠ» file_put_contents($lockFilename, $metaData['uri']); //Cron симуляция echo "Cron started\n"; sleep(20); echo "Cron stopped\n"; exit(); 

But the file remains, if you terminate the script via Ctrl + C

I thought to solve the problem through memcache and the lifetime of the key, but the free hosting doesn’t provide memcache

  • all right, you stopped the process, which means the code that gets the tmp file failed to run - Naumov
  • As an option to write a date to the file and if it is allowed yesterday to delete the file, but in general Π‘trl + C is not the correct completion of the program and it is better to track such cases - users
  • @Naumov from the documentation is simply a reference, The file is automatically removed when closed (for example, by calling fclose(), or when there are no remaining references to the file handle returned by tmpfile()), or when the script ends. If you create a file using WinApi CreateFile and the FILE_FLAG_DELETE_ON_CLOSE flag, even if you kill the process on the fly, the file will be deleted. Apparently everything is different in PHP and Linux ((( - Ninazu
  • 2
  • one
    @rjhdby the first option catches only errors, 2 working, but there are hosting sites where there is no work php pcntl - users

2 answers 2

Option with file lock:

 $fp = fopen("lock", "w+"); if (flock($fp, LOCK_EX | LOCK_NB)) { // выполняСм Π±Π»ΠΎΠΊΠΈΡ€ΠΎΠ²ΠΊΡƒ /* * Π’ΡƒΡ‚ ΠΊΠΎΠ΄ ΠΊΠΎΡ‚ΠΎΡ€Ρ‹ΠΉ Π½ΡƒΠΆΠ½ΠΎ Π²Ρ‹ΠΏΠΎΠ»Π½ΠΈΡ‚ΡŒ */ flock($fp, LOCK_UN); // ΠΎΡ‚ΠΏΠΈΡ€Π°Π΅ΠΌ Ρ„Π°ΠΉΠ» } else { echo "НС ΡƒΠ΄Π°Π»ΠΎΡΡŒ ΠΏΠΎΠ»ΡƒΡ‡ΠΈΡ‚ΡŒ Π±Π»ΠΎΠΊΠΈΡ€ΠΎΠ²ΠΊΡƒ, Ρ„Π°ΠΉΠ» ΡƒΠΆΠ΅ Π·Π°Π±Π»ΠΎΠΊΠΈΡ€ΠΎΠ²Π°Π½!"; } fclose($fp); unlink("lock"); 

Signal processing option:

 declare(ticks = 1); // Π²ΠΊΠ»ΡŽΡ‡ΠΈΡ‚ΡŒ ΠΎΠ±Ρ€Π°Π±ΠΎΡ‚ΠΊΡƒ сигнала function sigint() { exit(); } pcntl_signal(SIGINT, 'sigint'); pcntl_signal(SIGTERM, 'sigint'); /* * Π’ΡƒΡ‚ ΠΊΠΎΠ΄ ΠΊΠΎΡ‚ΠΎΡ€Ρ‹ΠΉ Π½ΡƒΠΆΠ½ΠΎ Π²Ρ‹ΠΏΠΎΠ»Π½ΠΈΡ‚ΡŒ */ 

    Absolutely hellish version. Create a table with one TIMESTAMP field and one record.

     $conn = new mysqli(....); $conn->begin_transaction(); $conn->query('UPDATE dummy SET stamp=NOW()'); ...... //самая послСдняя строка $conn->commit(); $conn->close(); 

    When the script terminates abruptly, it will rollback and unlock the table