1

Below is the code i'm using to run linux command it run but, doesn't show anything in terminal to set new password, group nothing. terminal just runs forever and doesn't exit.

 const child = execFile('sudo',['adduser', 'gku5'], (error, stdout, stderr) => { if (error) { throw error; } console.log(stdout); }); ``` 
1
  • 3
    I think the execFile isn't the intention here, which executes an external command and returns the buffered output. spawn is what you may be looking for. It creates a new shell and returns streaming I/O, with which you can interact. Do study this article to understand the difference. Also, this answer may help. Commented Jan 27, 2021 at 14:22

1 Answer 1

0

The command is not adduser, but useradd. For instance, i want to add new user named kbr. The command will be like this-

sudo useradd kbr 

Your program should look like this-

 const child = execFile('sudo',['useradd', 'gku5'], (error, stdout, stderr) => { if (error) { throw error; } console.log(stdout); });

And about the output. useradd shows output in RHEL, but it doesn't happen in other distribution. Please refer to this answer for this.

About the output, you can cat the auth.log file to show the output. For instance, cat /var/log/auth.log | tail -1 would show, something like this,

Jan 27 19:04:16 useradd[32328]: failed adding user 'kbr', data deleted 

So, the program should look like this-

child = exec('sudo useradd kbr | cat /var/log/auth.log | tail -1', function (error, stdout, stderr) { console.log('output: ' + stdout); if (error !== null) { console.log('exec error: ' + error); } });

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

3 Comments

I have tried using adduser same thing happens
Check this answer again, please.
this is the output i got output: Jan 27 19:42:49 vaita-desktop sudo: pam_unix(sudo:session): session opened for user root by (uid=0) exec error:

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.