0

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!

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, Josh! "Doesn't work on Windows" isn't a deal breaker since the app will never be deployed to that environment. In other cases, the exec allows provides a stop-gap solution that gives me time to migrate bash shell scripts to NodeJS.
Both native-exec and execpe appear forked from github.com/jprichardson/node-kexec. While native-exec is more popular, execpe looks more capable.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.