Execute a shell script on the remote server in the background Announcing the arrival of Valued...
How to write capital alpha?
Random body shuffle every night—can we still function?
Should a wizard buy fine inks every time he want to copy spells into his spellbook?
White walkers, cemeteries and wights
What is the difference between CTSS and ITS?
How can a team of shapeshifters communicate?
Does the Black Tentacles spell do damage twice at the start of turn to an already restrained creature?
Weaponising the Grasp-at-a-Distance spell
RSA find public exponent
Can two person see the same photon?
Universal covering space of the real projective line?
Special flights
Would color changing eyes affect vision?
Why do early math courses focus on the cross sections of a cone and not on other 3D objects?
What does 丫 mean? 丫是什么意思?
Tips to organize LaTeX presentations for a semester
New Order #6: Easter Egg
Moving a wrapfig vertically to encroach partially on a subsection title
After Sam didn't return home in the end, were he and Al still friends?
Printing attributes of selection in ArcPy?
What does it mean that physics no longer uses mechanical models to describe phenomena?
The test team as an enemy of development? And how can this be avoided?
How to ternary Plot3D a function
How can I prevent/balance waiting and turtling as a response to cooldown mechanics
Execute a shell script on the remote server in the background
Announcing the arrival of Valued Associate #679: Cesar Manara
Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern)
2019 Community Moderator Election Results
Why I closed the “Why is Kali so hard” questionRemote shell script Enter passphrase forLinux motion user - run it as ssh key as pi to remote serverHow to ssh and execute a remote script within a local shell script?SSH into a remote shell, execute a 'source' command, and stay in the remote shellExecute inline command remote server with sshScript wont return when executed via SSHSSH connections running in the background don't exit if multiple connections have been started by the same shellEnabling shell script to run as different user with PHPBash script -Way to ignore hung ServerHow to execute local script by passing arguments on remote via ssh as a different user?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ margin-bottom:0;
}
I have a list of servers and Bash scripts that need to be executed on a server. For example, I have a script that schedules a cronjob (with the command that the user has specified) on a server. This is just an example, I have the script for removing cronjob, storing SSH keys, configuring supervisor to start a daemon, etc.
I'm programatically SSHing into the server and want to execute those scripts on remote server, but "in the background". When I say "in the background", I mean that my code should finish and not worry about how long the actual script is running. This is because my code is running on a blocking language (PHP) and some scripts could potentially run few minutes. I want to send a script on the server through SSH, run it and don't worry about the rest -- I curl my server after a script has finished running (sort of like a webhook).
For preserving local filesystem storage, each script, after it has been modified with user content (for cronjob that could be the command and cron expression) is represented as a string, and not stored on my local server in a file. Example: I have a script echo "{{name}}" and user provides the "name" variable that programatically gets interpolated in the script. I don't want to store each script modification that the user has provided on my filesystem.
The way I've been doing this now is executing this command from my server:
ssh -T user@host /bin/bash <<EOFXn{{script}}EOFX
This is wrapped in a PHP command to execute shell commands and {{script}} gets replaced with this:
#!/bin/bash
cat << 'EOFY' > ./script.sh
{{executionScript}}
EOFY
. ./script.sh > ./script.out
curl --data "status=$(echo $?)" myserver.com
In this case, {{executionScript}} gets replaced with the modified version of final script that needs to be ran on a server (for example echo "John Doe")
This has been working great so far, but this SSH command waits for this script to finish running. If I put a sleep 4 before curl, my code (execution of the SSH command) will wait for 4 more seconds.
Is there a way to run the SSH command that needs to run a certain script in the background?
I've given you a high-level overview of what I'm trying to achieve in hope that somebody could potentially give a better solution to my problem (executing a set of user-modified scripts on the server). I know that this is a Unix/Linux board and my primary issue is with the SSH command that waits for the script to finish.
bash ssh
New contributor
crnkovic is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
add a comment |
I have a list of servers and Bash scripts that need to be executed on a server. For example, I have a script that schedules a cronjob (with the command that the user has specified) on a server. This is just an example, I have the script for removing cronjob, storing SSH keys, configuring supervisor to start a daemon, etc.
I'm programatically SSHing into the server and want to execute those scripts on remote server, but "in the background". When I say "in the background", I mean that my code should finish and not worry about how long the actual script is running. This is because my code is running on a blocking language (PHP) and some scripts could potentially run few minutes. I want to send a script on the server through SSH, run it and don't worry about the rest -- I curl my server after a script has finished running (sort of like a webhook).
For preserving local filesystem storage, each script, after it has been modified with user content (for cronjob that could be the command and cron expression) is represented as a string, and not stored on my local server in a file. Example: I have a script echo "{{name}}" and user provides the "name" variable that programatically gets interpolated in the script. I don't want to store each script modification that the user has provided on my filesystem.
The way I've been doing this now is executing this command from my server:
ssh -T user@host /bin/bash <<EOFXn{{script}}EOFX
This is wrapped in a PHP command to execute shell commands and {{script}} gets replaced with this:
#!/bin/bash
cat << 'EOFY' > ./script.sh
{{executionScript}}
EOFY
. ./script.sh > ./script.out
curl --data "status=$(echo $?)" myserver.com
In this case, {{executionScript}} gets replaced with the modified version of final script that needs to be ran on a server (for example echo "John Doe")
This has been working great so far, but this SSH command waits for this script to finish running. If I put a sleep 4 before curl, my code (execution of the SSH command) will wait for 4 more seconds.
Is there a way to run the SSH command that needs to run a certain script in the background?
I've given you a high-level overview of what I'm trying to achieve in hope that somebody could potentially give a better solution to my problem (executing a set of user-modified scripts on the server). I know that this is a Unix/Linux board and my primary issue is with the SSH command that waits for the script to finish.
bash ssh
New contributor
crnkovic is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
You are basically trying to re-invent Ansible (or a subset thereof). Check it out.
– xenoid
2 hours ago
I know of these services. That's not what I asked. I'm building this project as a part of own learning experience.
– crnkovic
2 hours ago
Why are you sourcing (. ./script.sh) the script instead of executing it (./script.sh)?
– terdon♦
2 hours ago
I want to curl after a script has finished, therefore I would need to run it in a current shell, if I want to curl afterwards, right? I'm not a linux guy, not much experience :-)
– crnkovic
2 hours ago
No, that shouldn't make any difference. Thecurlwould wait for the script to finish either way. This isn't a problem, it's just odd and makes it slightly more complcated than it needs to be.
– terdon♦
2 hours ago
add a comment |
I have a list of servers and Bash scripts that need to be executed on a server. For example, I have a script that schedules a cronjob (with the command that the user has specified) on a server. This is just an example, I have the script for removing cronjob, storing SSH keys, configuring supervisor to start a daemon, etc.
I'm programatically SSHing into the server and want to execute those scripts on remote server, but "in the background". When I say "in the background", I mean that my code should finish and not worry about how long the actual script is running. This is because my code is running on a blocking language (PHP) and some scripts could potentially run few minutes. I want to send a script on the server through SSH, run it and don't worry about the rest -- I curl my server after a script has finished running (sort of like a webhook).
For preserving local filesystem storage, each script, after it has been modified with user content (for cronjob that could be the command and cron expression) is represented as a string, and not stored on my local server in a file. Example: I have a script echo "{{name}}" and user provides the "name" variable that programatically gets interpolated in the script. I don't want to store each script modification that the user has provided on my filesystem.
The way I've been doing this now is executing this command from my server:
ssh -T user@host /bin/bash <<EOFXn{{script}}EOFX
This is wrapped in a PHP command to execute shell commands and {{script}} gets replaced with this:
#!/bin/bash
cat << 'EOFY' > ./script.sh
{{executionScript}}
EOFY
. ./script.sh > ./script.out
curl --data "status=$(echo $?)" myserver.com
In this case, {{executionScript}} gets replaced with the modified version of final script that needs to be ran on a server (for example echo "John Doe")
This has been working great so far, but this SSH command waits for this script to finish running. If I put a sleep 4 before curl, my code (execution of the SSH command) will wait for 4 more seconds.
Is there a way to run the SSH command that needs to run a certain script in the background?
I've given you a high-level overview of what I'm trying to achieve in hope that somebody could potentially give a better solution to my problem (executing a set of user-modified scripts on the server). I know that this is a Unix/Linux board and my primary issue is with the SSH command that waits for the script to finish.
bash ssh
New contributor
crnkovic is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
I have a list of servers and Bash scripts that need to be executed on a server. For example, I have a script that schedules a cronjob (with the command that the user has specified) on a server. This is just an example, I have the script for removing cronjob, storing SSH keys, configuring supervisor to start a daemon, etc.
I'm programatically SSHing into the server and want to execute those scripts on remote server, but "in the background". When I say "in the background", I mean that my code should finish and not worry about how long the actual script is running. This is because my code is running on a blocking language (PHP) and some scripts could potentially run few minutes. I want to send a script on the server through SSH, run it and don't worry about the rest -- I curl my server after a script has finished running (sort of like a webhook).
For preserving local filesystem storage, each script, after it has been modified with user content (for cronjob that could be the command and cron expression) is represented as a string, and not stored on my local server in a file. Example: I have a script echo "{{name}}" and user provides the "name" variable that programatically gets interpolated in the script. I don't want to store each script modification that the user has provided on my filesystem.
The way I've been doing this now is executing this command from my server:
ssh -T user@host /bin/bash <<EOFXn{{script}}EOFX
This is wrapped in a PHP command to execute shell commands and {{script}} gets replaced with this:
#!/bin/bash
cat << 'EOFY' > ./script.sh
{{executionScript}}
EOFY
. ./script.sh > ./script.out
curl --data "status=$(echo $?)" myserver.com
In this case, {{executionScript}} gets replaced with the modified version of final script that needs to be ran on a server (for example echo "John Doe")
This has been working great so far, but this SSH command waits for this script to finish running. If I put a sleep 4 before curl, my code (execution of the SSH command) will wait for 4 more seconds.
Is there a way to run the SSH command that needs to run a certain script in the background?
I've given you a high-level overview of what I'm trying to achieve in hope that somebody could potentially give a better solution to my problem (executing a set of user-modified scripts on the server). I know that this is a Unix/Linux board and my primary issue is with the SSH command that waits for the script to finish.
bash ssh
bash ssh
New contributor
crnkovic is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
crnkovic is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
crnkovic is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
asked 2 hours ago
crnkoviccrnkovic
61
61
New contributor
crnkovic is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
crnkovic is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
crnkovic is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
You are basically trying to re-invent Ansible (or a subset thereof). Check it out.
– xenoid
2 hours ago
I know of these services. That's not what I asked. I'm building this project as a part of own learning experience.
– crnkovic
2 hours ago
Why are you sourcing (. ./script.sh) the script instead of executing it (./script.sh)?
– terdon♦
2 hours ago
I want to curl after a script has finished, therefore I would need to run it in a current shell, if I want to curl afterwards, right? I'm not a linux guy, not much experience :-)
– crnkovic
2 hours ago
No, that shouldn't make any difference. Thecurlwould wait for the script to finish either way. This isn't a problem, it's just odd and makes it slightly more complcated than it needs to be.
– terdon♦
2 hours ago
add a comment |
You are basically trying to re-invent Ansible (or a subset thereof). Check it out.
– xenoid
2 hours ago
I know of these services. That's not what I asked. I'm building this project as a part of own learning experience.
– crnkovic
2 hours ago
Why are you sourcing (. ./script.sh) the script instead of executing it (./script.sh)?
– terdon♦
2 hours ago
I want to curl after a script has finished, therefore I would need to run it in a current shell, if I want to curl afterwards, right? I'm not a linux guy, not much experience :-)
– crnkovic
2 hours ago
No, that shouldn't make any difference. Thecurlwould wait for the script to finish either way. This isn't a problem, it's just odd and makes it slightly more complcated than it needs to be.
– terdon♦
2 hours ago
You are basically trying to re-invent Ansible (or a subset thereof). Check it out.
– xenoid
2 hours ago
You are basically trying to re-invent Ansible (or a subset thereof). Check it out.
– xenoid
2 hours ago
I know of these services. That's not what I asked. I'm building this project as a part of own learning experience.
– crnkovic
2 hours ago
I know of these services. That's not what I asked. I'm building this project as a part of own learning experience.
– crnkovic
2 hours ago
Why are you sourcing (
. ./script.sh) the script instead of executing it (./script.sh)?– terdon♦
2 hours ago
Why are you sourcing (
. ./script.sh) the script instead of executing it (./script.sh)?– terdon♦
2 hours ago
I want to curl after a script has finished, therefore I would need to run it in a current shell, if I want to curl afterwards, right? I'm not a linux guy, not much experience :-)
– crnkovic
2 hours ago
I want to curl after a script has finished, therefore I would need to run it in a current shell, if I want to curl afterwards, right? I'm not a linux guy, not much experience :-)
– crnkovic
2 hours ago
No, that shouldn't make any difference. The
curl would wait for the script to finish either way. This isn't a problem, it's just odd and makes it slightly more complcated than it needs to be.– terdon♦
2 hours ago
No, that shouldn't make any difference. The
curl would wait for the script to finish either way. This isn't a problem, it's just odd and makes it slightly more complcated than it needs to be.– terdon♦
2 hours ago
add a comment |
0
active
oldest
votes
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "106"
};
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: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
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
});
}
});
crnkovic is a new contributor. Be nice, and check out our Code of Conduct.
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%2funix.stackexchange.com%2fquestions%2f513603%2fexecute-a-shell-script-on-the-remote-server-in-the-background%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
crnkovic is a new contributor. Be nice, and check out our Code of Conduct.
crnkovic is a new contributor. Be nice, and check out our Code of Conduct.
crnkovic is a new contributor. Be nice, and check out our Code of Conduct.
crnkovic is a new contributor. Be nice, and check out our Code of Conduct.
Thanks for contributing an answer to Unix & Linux Stack Exchange!
- 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%2funix.stackexchange.com%2fquestions%2f513603%2fexecute-a-shell-script-on-the-remote-server-in-the-background%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
You are basically trying to re-invent Ansible (or a subset thereof). Check it out.
– xenoid
2 hours ago
I know of these services. That's not what I asked. I'm building this project as a part of own learning experience.
– crnkovic
2 hours ago
Why are you sourcing (
. ./script.sh) the script instead of executing it (./script.sh)?– terdon♦
2 hours ago
I want to curl after a script has finished, therefore I would need to run it in a current shell, if I want to curl afterwards, right? I'm not a linux guy, not much experience :-)
– crnkovic
2 hours ago
No, that shouldn't make any difference. The
curlwould wait for the script to finish either way. This isn't a problem, it's just odd and makes it slightly more complcated than it needs to be.– terdon♦
2 hours ago