Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9162583
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T14:14:43+00:00 2026-06-17T14:14:43+00:00

How to exec continuously? e.g. ls after cd ? I tried exec = require(‘child_process’).exec;

  • 0

How to exec continuously? e.g. ls after cd?

I tried

exec = require('child_process').exec;
exec('cd ~/', 
  function(){
    exec('ls'),
    function(err, stdout, stderr){
      console.log(stdout); // this logs current dir but not ~/'s
    }
  }
)

exec('cd ~/').exec('ls', function(err, stdout, stderr){
  console.log(stdout);
})//this also fails because first exec returns a ChildProcess Object but not itself.
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T14:14:44+00:00Added an answer on June 17, 2026 at 2:14 pm

    It is not possible to do this because exec and spawn creates a new process. But there is a way to simulate this. You can start a process with exec and execute multiple commands in the same time:
    In the command line if you want to execute 3 commands on the same line you would write:

    cmd1 & cmd2 & cmd3
    

    So, all 3 commands run in the same process and have access to the context modified by the previous executed commands.
    Let’s take your example, you want to execute cd ../ and after that to execute dir and to view the previous directory list.
    In cmd you shoud write:

    cd../ & dir
    

    From node js you can start a process with exec and to tell it to start another node instance that will evaluate an inline script:

    var exec = require('child_process').exec;
    var script = "var exec = require('child_process').exec;exec('dir',function(e,d,er){console.log(d);});";
    script = '"'+script+'"';//enclose the inline script with "" because it contains spaces
    var cmd2 = 'node -e '+script;
    var cd = exec('cd ../ &'+cmd2,function(err,stdout,strerr)
    {
        console.log(stdout);//this would work
    })
    

    If you just want to change the current directory you should check the documentation about it http://nodejs.org/api/child_process.html#child_process_child_process_exec_command_options_callback

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

After creating table using exec() in PDO,it shows like this Array ( [0] =>
I read an exec'd program's stdout using a pipe: int pipes[2]; pipe(pipes); if (fork()
shell_exec() : I'm doing a PHP site that uses a shell_exec() function like this:
Why does this work: exec 3<>/dev/tcp/www.google.com/80 echo -e GET / HTTP/1.1\n\n>&3 cat <&3 And
PHP exec() . can i use this to export data to csv with a
The exec() function works for changing file attributes in Windows machine as long as
I got a process which runs under unix (shell-script). This script runs continuously and
exec 3>&1 # Save current value of stdout. ls -l 2>&1 >&3 3>&- |
I've used the exec() function a few times in the past to capture information
If you run the exec stored procedure command in SSMS, it produces this script:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.