node.jschild-processxclip

node child_process execSync hangs with xclip


I'm trying to use child_process to execute xclip -selection c, but it seems to hang or significantly delay execution.

I've tried using execSync,

require('child_process').execSync('echo hi | xclip -selection c') && console.log('done');

I've also tried using exec,

require('child_process').exec('echo hi | xclip -selection c', (a) => console.log('done', a)) && undefined;

In both cases, there is a noticeable delay between pressing enter and when done is printed. However, the clipboard is actually affected almost immediately, it seems node just doesn't seem to realize the command is complete.

Also to note, the delay seems to vary between executions. Plus, the exec variant seems to delay for less time than the execSync variant which sometimes seems to hang indefinitely.


Solution

  • Apologies for answering my own question, but I stumbled upon the answer shortly after posting the question.

    Apparently xclip, by default, doesn't terminate when invoked, but keeps listening for more input. To instruct xclip to only expect 1 input requires the -l argument, e.g.:

    require('child_process').execSync('echo hi | xclip -selection c -l') && console.log('done');
    

    source: https://github.com/astrand/xclip/issues/45