Running a sh script in the background, and adding the job to the list of jobs
I want to run a NodeJS server for my server.
I installed Nodemon, and created a script that will run it automatically.
This is the content of my init.sh
script:
nodemon ./index.js > logs 2> errors &
But when I run it with:
./init.sh
And then run:
jobs
I don't see it in the list of jobs. I have to run:
ps -eaf | grep nodemon
To get the ID of the job, then kill it.
I've been looking for a solution, and it seems it is related to the context of the shell I'm in (I'm using SSH).
Could someone help me achieve what I am trying to do (i.e. send the job to the list of jobs), or at least point me to the right direction?
linux bash ssh
add a comment |
I want to run a NodeJS server for my server.
I installed Nodemon, and created a script that will run it automatically.
This is the content of my init.sh
script:
nodemon ./index.js > logs 2> errors &
But when I run it with:
./init.sh
And then run:
jobs
I don't see it in the list of jobs. I have to run:
ps -eaf | grep nodemon
To get the ID of the job, then kill it.
I've been looking for a solution, and it seems it is related to the context of the shell I'm in (I'm using SSH).
Could someone help me achieve what I am trying to do (i.e. send the job to the list of jobs), or at least point me to the right direction?
linux bash ssh
When you say, “… it seems it is related to the context of the shell I'm in (I'm using SSH).” You are mixing up your connection method (SSH) with the shell which is 100% not related. If yourinit.sh
is as it is shown here you are using the basic shell which is/bin/sh
which can—honestly—be limited. Try adding#!/bin/bash -l
to the top of yourinit.sh
to make the script use the Bash shell and the-l
passes along environment variables which might be useful.
– JakeGould
Jan 22 at 11:38
add a comment |
I want to run a NodeJS server for my server.
I installed Nodemon, and created a script that will run it automatically.
This is the content of my init.sh
script:
nodemon ./index.js > logs 2> errors &
But when I run it with:
./init.sh
And then run:
jobs
I don't see it in the list of jobs. I have to run:
ps -eaf | grep nodemon
To get the ID of the job, then kill it.
I've been looking for a solution, and it seems it is related to the context of the shell I'm in (I'm using SSH).
Could someone help me achieve what I am trying to do (i.e. send the job to the list of jobs), or at least point me to the right direction?
linux bash ssh
I want to run a NodeJS server for my server.
I installed Nodemon, and created a script that will run it automatically.
This is the content of my init.sh
script:
nodemon ./index.js > logs 2> errors &
But when I run it with:
./init.sh
And then run:
jobs
I don't see it in the list of jobs. I have to run:
ps -eaf | grep nodemon
To get the ID of the job, then kill it.
I've been looking for a solution, and it seems it is related to the context of the shell I'm in (I'm using SSH).
Could someone help me achieve what I am trying to do (i.e. send the job to the list of jobs), or at least point me to the right direction?
linux bash ssh
linux bash ssh
edited Jan 22 at 11:32
JakeGould
31.4k1096138
31.4k1096138
asked Jan 22 at 11:27
trichetrichetrichetriche
1033
1033
When you say, “… it seems it is related to the context of the shell I'm in (I'm using SSH).” You are mixing up your connection method (SSH) with the shell which is 100% not related. If yourinit.sh
is as it is shown here you are using the basic shell which is/bin/sh
which can—honestly—be limited. Try adding#!/bin/bash -l
to the top of yourinit.sh
to make the script use the Bash shell and the-l
passes along environment variables which might be useful.
– JakeGould
Jan 22 at 11:38
add a comment |
When you say, “… it seems it is related to the context of the shell I'm in (I'm using SSH).” You are mixing up your connection method (SSH) with the shell which is 100% not related. If yourinit.sh
is as it is shown here you are using the basic shell which is/bin/sh
which can—honestly—be limited. Try adding#!/bin/bash -l
to the top of yourinit.sh
to make the script use the Bash shell and the-l
passes along environment variables which might be useful.
– JakeGould
Jan 22 at 11:38
When you say, “… it seems it is related to the context of the shell I'm in (I'm using SSH).” You are mixing up your connection method (SSH) with the shell which is 100% not related. If your
init.sh
is as it is shown here you are using the basic shell which is /bin/sh
which can—honestly—be limited. Try adding #!/bin/bash -l
to the top of your init.sh
to make the script use the Bash shell and the -l
passes along environment variables which might be useful.– JakeGould
Jan 22 at 11:38
When you say, “… it seems it is related to the context of the shell I'm in (I'm using SSH).” You are mixing up your connection method (SSH) with the shell which is 100% not related. If your
init.sh
is as it is shown here you are using the basic shell which is /bin/sh
which can—honestly—be limited. Try adding #!/bin/bash -l
to the top of your init.sh
to make the script use the Bash shell and the -l
passes along environment variables which might be useful.– JakeGould
Jan 22 at 11:38
add a comment |
1 Answer
1
active
oldest
votes
jobs
prints jobs of your current shell, while ./init.sh
runs the script within a separate shell. In this separate shell job control is probably disabled, but it doesn't matter because you can't easily use it from your current shell anyway.
To "run" the script in your current shell, source it:
. init.sh
This is equivalent to typing the content of the file in your current shell. Then jobs
should show nodemon
as a job of the current shell.
Seems to work, thank you ! What I apparently failed to understand and that I can see now is that the job is actually stopped. is there a way to keep it running ? Also, would you have a solution to avoid having to use the.
before the command ? I have read aboutnohup
, would that solve my issue ?
– trichetriche
Jan 22 at 11:50
@trichetriche Please see Linux process in background - “Stopped” in jobs?nohup
and input redirection make the most upvoted answer there, but it's the redirection that does the trick. Note your command may or may not terminate immediately with input redirected from/dev/null
(I don't knownodemon
, I'm unable to predict its behavior).
– Kamil Maciorowski
Jan 22 at 11:57
@trichetriche I don't think you can avoid.
(orsource
) if you want to use job control of the current shell.
– Kamil Maciorowski
Jan 22 at 12:02
Nodemon simply is anode
command with a watcher so that you don't have to re-run your node command at every change. If I got it correctly, I am supposed to redirect the outputs to files, but I'm already doing it, so why would it stop ? Anyway, I've decided to run the command directly for now, as I don't want to focus on line but the JS itself. Thank you anyway for your help, at least i've learn how to source a script !
– trichetriche
Jan 22 at 12:04
1
Oh okay, sorry about that, thank you very much :)
– trichetriche
Jan 22 at 12:31
|
show 1 more comment
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "3"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsuperuser.com%2fquestions%2f1396979%2frunning-a-sh-script-in-the-background-and-adding-the-job-to-the-list-of-jobs%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
jobs
prints jobs of your current shell, while ./init.sh
runs the script within a separate shell. In this separate shell job control is probably disabled, but it doesn't matter because you can't easily use it from your current shell anyway.
To "run" the script in your current shell, source it:
. init.sh
This is equivalent to typing the content of the file in your current shell. Then jobs
should show nodemon
as a job of the current shell.
Seems to work, thank you ! What I apparently failed to understand and that I can see now is that the job is actually stopped. is there a way to keep it running ? Also, would you have a solution to avoid having to use the.
before the command ? I have read aboutnohup
, would that solve my issue ?
– trichetriche
Jan 22 at 11:50
@trichetriche Please see Linux process in background - “Stopped” in jobs?nohup
and input redirection make the most upvoted answer there, but it's the redirection that does the trick. Note your command may or may not terminate immediately with input redirected from/dev/null
(I don't knownodemon
, I'm unable to predict its behavior).
– Kamil Maciorowski
Jan 22 at 11:57
@trichetriche I don't think you can avoid.
(orsource
) if you want to use job control of the current shell.
– Kamil Maciorowski
Jan 22 at 12:02
Nodemon simply is anode
command with a watcher so that you don't have to re-run your node command at every change. If I got it correctly, I am supposed to redirect the outputs to files, but I'm already doing it, so why would it stop ? Anyway, I've decided to run the command directly for now, as I don't want to focus on line but the JS itself. Thank you anyway for your help, at least i've learn how to source a script !
– trichetriche
Jan 22 at 12:04
1
Oh okay, sorry about that, thank you very much :)
– trichetriche
Jan 22 at 12:31
|
show 1 more comment
jobs
prints jobs of your current shell, while ./init.sh
runs the script within a separate shell. In this separate shell job control is probably disabled, but it doesn't matter because you can't easily use it from your current shell anyway.
To "run" the script in your current shell, source it:
. init.sh
This is equivalent to typing the content of the file in your current shell. Then jobs
should show nodemon
as a job of the current shell.
Seems to work, thank you ! What I apparently failed to understand and that I can see now is that the job is actually stopped. is there a way to keep it running ? Also, would you have a solution to avoid having to use the.
before the command ? I have read aboutnohup
, would that solve my issue ?
– trichetriche
Jan 22 at 11:50
@trichetriche Please see Linux process in background - “Stopped” in jobs?nohup
and input redirection make the most upvoted answer there, but it's the redirection that does the trick. Note your command may or may not terminate immediately with input redirected from/dev/null
(I don't knownodemon
, I'm unable to predict its behavior).
– Kamil Maciorowski
Jan 22 at 11:57
@trichetriche I don't think you can avoid.
(orsource
) if you want to use job control of the current shell.
– Kamil Maciorowski
Jan 22 at 12:02
Nodemon simply is anode
command with a watcher so that you don't have to re-run your node command at every change. If I got it correctly, I am supposed to redirect the outputs to files, but I'm already doing it, so why would it stop ? Anyway, I've decided to run the command directly for now, as I don't want to focus on line but the JS itself. Thank you anyway for your help, at least i've learn how to source a script !
– trichetriche
Jan 22 at 12:04
1
Oh okay, sorry about that, thank you very much :)
– trichetriche
Jan 22 at 12:31
|
show 1 more comment
jobs
prints jobs of your current shell, while ./init.sh
runs the script within a separate shell. In this separate shell job control is probably disabled, but it doesn't matter because you can't easily use it from your current shell anyway.
To "run" the script in your current shell, source it:
. init.sh
This is equivalent to typing the content of the file in your current shell. Then jobs
should show nodemon
as a job of the current shell.
jobs
prints jobs of your current shell, while ./init.sh
runs the script within a separate shell. In this separate shell job control is probably disabled, but it doesn't matter because you can't easily use it from your current shell anyway.
To "run" the script in your current shell, source it:
. init.sh
This is equivalent to typing the content of the file in your current shell. Then jobs
should show nodemon
as a job of the current shell.
answered Jan 22 at 11:45
Kamil MaciorowskiKamil Maciorowski
27.3k155982
27.3k155982
Seems to work, thank you ! What I apparently failed to understand and that I can see now is that the job is actually stopped. is there a way to keep it running ? Also, would you have a solution to avoid having to use the.
before the command ? I have read aboutnohup
, would that solve my issue ?
– trichetriche
Jan 22 at 11:50
@trichetriche Please see Linux process in background - “Stopped” in jobs?nohup
and input redirection make the most upvoted answer there, but it's the redirection that does the trick. Note your command may or may not terminate immediately with input redirected from/dev/null
(I don't knownodemon
, I'm unable to predict its behavior).
– Kamil Maciorowski
Jan 22 at 11:57
@trichetriche I don't think you can avoid.
(orsource
) if you want to use job control of the current shell.
– Kamil Maciorowski
Jan 22 at 12:02
Nodemon simply is anode
command with a watcher so that you don't have to re-run your node command at every change. If I got it correctly, I am supposed to redirect the outputs to files, but I'm already doing it, so why would it stop ? Anyway, I've decided to run the command directly for now, as I don't want to focus on line but the JS itself. Thank you anyway for your help, at least i've learn how to source a script !
– trichetriche
Jan 22 at 12:04
1
Oh okay, sorry about that, thank you very much :)
– trichetriche
Jan 22 at 12:31
|
show 1 more comment
Seems to work, thank you ! What I apparently failed to understand and that I can see now is that the job is actually stopped. is there a way to keep it running ? Also, would you have a solution to avoid having to use the.
before the command ? I have read aboutnohup
, would that solve my issue ?
– trichetriche
Jan 22 at 11:50
@trichetriche Please see Linux process in background - “Stopped” in jobs?nohup
and input redirection make the most upvoted answer there, but it's the redirection that does the trick. Note your command may or may not terminate immediately with input redirected from/dev/null
(I don't knownodemon
, I'm unable to predict its behavior).
– Kamil Maciorowski
Jan 22 at 11:57
@trichetriche I don't think you can avoid.
(orsource
) if you want to use job control of the current shell.
– Kamil Maciorowski
Jan 22 at 12:02
Nodemon simply is anode
command with a watcher so that you don't have to re-run your node command at every change. If I got it correctly, I am supposed to redirect the outputs to files, but I'm already doing it, so why would it stop ? Anyway, I've decided to run the command directly for now, as I don't want to focus on line but the JS itself. Thank you anyway for your help, at least i've learn how to source a script !
– trichetriche
Jan 22 at 12:04
1
Oh okay, sorry about that, thank you very much :)
– trichetriche
Jan 22 at 12:31
Seems to work, thank you ! What I apparently failed to understand and that I can see now is that the job is actually stopped. is there a way to keep it running ? Also, would you have a solution to avoid having to use the
.
before the command ? I have read about nohup
, would that solve my issue ?– trichetriche
Jan 22 at 11:50
Seems to work, thank you ! What I apparently failed to understand and that I can see now is that the job is actually stopped. is there a way to keep it running ? Also, would you have a solution to avoid having to use the
.
before the command ? I have read about nohup
, would that solve my issue ?– trichetriche
Jan 22 at 11:50
@trichetriche Please see Linux process in background - “Stopped” in jobs?
nohup
and input redirection make the most upvoted answer there, but it's the redirection that does the trick. Note your command may or may not terminate immediately with input redirected from /dev/null
(I don't know nodemon
, I'm unable to predict its behavior).– Kamil Maciorowski
Jan 22 at 11:57
@trichetriche Please see Linux process in background - “Stopped” in jobs?
nohup
and input redirection make the most upvoted answer there, but it's the redirection that does the trick. Note your command may or may not terminate immediately with input redirected from /dev/null
(I don't know nodemon
, I'm unable to predict its behavior).– Kamil Maciorowski
Jan 22 at 11:57
@trichetriche I don't think you can avoid
.
(or source
) if you want to use job control of the current shell.– Kamil Maciorowski
Jan 22 at 12:02
@trichetriche I don't think you can avoid
.
(or source
) if you want to use job control of the current shell.– Kamil Maciorowski
Jan 22 at 12:02
Nodemon simply is a
node
command with a watcher so that you don't have to re-run your node command at every change. If I got it correctly, I am supposed to redirect the outputs to files, but I'm already doing it, so why would it stop ? Anyway, I've decided to run the command directly for now, as I don't want to focus on line but the JS itself. Thank you anyway for your help, at least i've learn how to source a script !– trichetriche
Jan 22 at 12:04
Nodemon simply is a
node
command with a watcher so that you don't have to re-run your node command at every change. If I got it correctly, I am supposed to redirect the outputs to files, but I'm already doing it, so why would it stop ? Anyway, I've decided to run the command directly for now, as I don't want to focus on line but the JS itself. Thank you anyway for your help, at least i've learn how to source a script !– trichetriche
Jan 22 at 12:04
1
1
Oh okay, sorry about that, thank you very much :)
– trichetriche
Jan 22 at 12:31
Oh okay, sorry about that, thank you very much :)
– trichetriche
Jan 22 at 12:31
|
show 1 more comment
Thanks for contributing an answer to Super User!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsuperuser.com%2fquestions%2f1396979%2frunning-a-sh-script-in-the-background-and-adding-the-job-to-the-list-of-jobs%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
When you say, “… it seems it is related to the context of the shell I'm in (I'm using SSH).” You are mixing up your connection method (SSH) with the shell which is 100% not related. If your
init.sh
is as it is shown here you are using the basic shell which is/bin/sh
which can—honestly—be limited. Try adding#!/bin/bash -l
to the top of yourinit.sh
to make the script use the Bash shell and the-l
passes along environment variables which might be useful.– JakeGould
Jan 22 at 11:38