Basic Ubuntu Bash Script Not Running [duplicate] - bash

This question already has answers here:
How do I run a shell script without using "sh" or "bash" commands?
(13 answers)
Add a bash script to path
(5 answers)
How to make a shell script global?
(9 answers)
Closed 4 years ago.
As I'm sure you'll all tell very soon, I'm no expert when it comes to this sort of thing. However, when trying to look at results, I've not been able to come across anything quite like what I need - so I decided to ask here.
BACKGROUND: When I log into my server, I need to cd into a folder and run some commands based on one of the 3 things I want to do:
1) Stop the server
2) Start the server
3) Rebuild the server
AIM: Rebuilding the server takes 2 commands, one of them (to rebuild) takes some time and instead of me having to cd into the directory, stop the server, rebuild the script, then start it again... I'm trying to create a bash file to do it for me, so I can simply login and execute one command.
The directory I need to CD into is: /var/www/website/
The stop process I need to enter is: pm2 stop 0
The Rebuild process I need to enter is: yarn run build -- --release
The start process I need to enter is: pm2 start build/server.js
When I created a restartserver.sh file, I created it as follows:
#!/bin/bash
cd /var/www/website
pm2 stop 0
yarn run build -- --release
pm2 start build/server.js
When i try to run it by typing 'restartserver.sh' I get the error:
-bash: restartserver.sh: command not found
I'm sure this is really easy for someone who has any idea about this sort of thing... but for my... I've no idea. Can anyone please help?
Thanks
P

Related

How to see bash script executed command? [duplicate]

This question already has answers here:
How can I debug a Bash script? [closed]
(12 answers)
Closed 2 years ago.
I'm not sure I address this problem correctly, but I've search and search over the net and found nothing, yet.
I'm writing a bash script and I want to see what the script is really doing when being executed, like a log of all the commands, one by one, what are executed - this way, I'll be able to see why my script is falling.
Note : I've post similar question in the past, someone told me I run my bash script with sh -xe script.sh but it doesn't give me enough information to debug properly.
Please advise. Thanks!
Adding set -x at the beginning of the script displays, in the terminal, all the commands sent by the script as the terminal received it. It was exactly what I needed and it working perfectly.

How to execute an .exe file including blank spaces in it [duplicate]

This question already has answers here:
How to create batch file in Windows using "start" with a path and command with spaces
(7 answers)
Closed 2 years ago.
So normally I have several *.bat files to automate some things in my computer.
But I'm kinda stuck in this call...
cd "C:\Users\user\AppData\Local\Programs\program2Execute"
start program has spaces.exe
I don't get the app executed, instead I get a cmd window with the cd command result, nothing else. I tried to quote "program has spaces.exe" but it doesn't get executed I get the window duplicated instead.
Apologize if this is kinda dumb to ask, but I have spent quite time looking for the answer.
Thanks in advance.
If you use the start command:
start "" "a program with spaces.exe"
start will use the first double quoted string as the title and the 2nd as the command.
Or don't use start:
"a program with spaces.exe"

why bash shell does not make any difference after executed? [duplicate]

This question already has answers here:
Global environment variables in a shell script
(7 answers)
Closed 5 years ago.
I'm working a small project which needs using OpenMPI to make "mpicc" work.
I made a file make_cmd:
#!/bin/bash
module load OpenMPI
However, after executing ./make_cmd, I was told:
mpicc: command not found
But if I just type on the command line: module load OpenMPI, then mpicc is working.
Why is that? Thanks!
See this answer on neighbouring site.
Because module is an alias/shell function and not a binary program, it's not necessarily available in the non-interactive sub-shell that is created when you run your script. You could probably run source make_cmd though, as that will just run the commands in your current interactive shell. You could ditch the #!/bin/bash line in that case.

Launch shell script from another script [duplicate]

This question already has an answer here:
Running several scripts in parallel bash script [duplicate]
(1 answer)
Closed 7 years ago.
I have the following shell script
#for i in {0..10}
do
run my command that takes about 10 seconds with parameter $i
done
How can I get this to run the commands in parallel without using GNU Parallel as I am not able to install it on my linux box.
Is there a way I can create 10 different shell scripts and call them e.g. script_1.sh, script_2.sh, script_3.sh etc and then launch them one by one from this script?
You could use an ampersand (&) and launch script.sh $1 & ten times. This will make the script run in a fork of the main process. It is an easy way to do parallel processing but definitely not very flexible and doesn't have many features. A simple tutorial can be found here.

Regarding operation flow in a bash script [duplicate]

This question already has answers here:
bash script order of execution
(2 answers)
Closed 7 years ago.
I was just wondering, in a bash script, if I have a command on one line to say rsync a large file (~10GB) then the next command on the next line is meant to rename then move that same file, will the script know to wait for the rsync to complete before attempting the rename and move?
Is there a flag or something I can put on each line to make it wait before executing the next command?
Sorry if this seems like a total noob question, but alas I am a noob!
Thanks in advance for any info!
All commands are executed in sequence, so, a command will wait for the previous command to complete. Only when you add a & to the end of a command will it run in the background, and thus not wait for completion before executing the next.

Resources