Michael Mikowski
Michael Mikowski

Reputation: 1279

Is there a Node JS equivalent to Bash exec?

There are times when I want to exit a NodeJS script and hand over all control of the terminal to another program just like Bash's exec command. This behavior is shown below using the simple t.sh:

#!/bin/bash
sleep 5; exec /usr/bin/vlc

We can start t.sh and then press ctrl-z to suspend the process and inspect the PID:

ps -elf |grep t.sh |grep -v grep |awk '{print $4}' # prints 10554

We can then resume the process using fg and, after VLC starts, press ctrl-z once again and inspect the PID of VLC:

ps -elf |grep vlc |grep -v grep |awk '{print $4}' # prints 10554

We can see VLC has replaced t.sh altogether using the PID. I very much want the same behavior with Node JS but am haven't discovered the magic yet.

Something like this would be perfect: process.exit( '/usr/bin/vlc' ); Of course, that doesn't work. I am already using child_process.spawn and child_process.exec and process.exit in other capacities. Unless I'm missing something, these don't appear to be able to do a classic Bash exec. Help!

Upvotes: 0

Views: 600

Answers (1)

josh3736
josh3736

Reputation: 145002

node does not have any built-in access to the necessary APIs (as they are not portable to Windows), but there are a few packages that provide native bindings: execpe, native-exec.

Upvotes: 1

Related Questions