File size: 1,293 Bytes
bad179a
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
// The delayed-background pattern of "Reboot" / "Shut down" ((sleep 2; systemctl reboot) >/dev/null 2>&1 &) must survive the end of its
// command and a reconnect, now that children are ended by process group. Uses a harmless marker file instead. node t_bg.js <port> <scratch file>
const fs = require('fs'), { sleep, client, check, done } = require('./lib'), [port, mark] = process.argv.slice(2), { post } = client(port);
(async () => {
    for (const f of [mark + '.1', mark + '.2']) fs.existsSync(f) && fs.unlinkSync(f);
    let t = Date.now(), r = await post('exec', { id: 'local1', cmd: `(sleep 2; echo ran > '${mark}.1') >/dev/null 2>&1 & echo scheduled`, privileged: true });
    check('the command returns at once', r.code === 0 && r.out === 'scheduled\n' && Date.now() - t < 1000, `${JSON.stringify(r)} after ${Date.now() - t} ms`);
    r = await post('exec', { id: 'local1', cmd: `(sleep 2; echo ran > '${mark}.2') >/dev/null 2>&1 & echo scheduled` });
    await post('server/reconnect', { id: 'local1' }); // drops the link right after the command ended
    await sleep(3500);
    check('the delayed part still runs (also after a reconnect)', fs.existsSync(mark + '.1') && fs.existsSync(mark + '.2'), `${fs.existsSync(mark + '.1')} ${fs.existsSync(mark + '.2')}`);
    done();
})();