0

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

Charles Duffy
  • 280,126
  • 43
  • 390
  • 441
Paul
  • 17
  • 7
  • 3
    Make it executable `chmod +x restartserver.sh` and execute it like `./restartserver.sh` – JNevill Jul 18 '18 at 16:23
  • BTW, typically you'd just want your command (and thus your script) to be called `restartserver`, not `restartserver.sh`. Scripts in the PATH define commands, and you run `ls`, not `ls.elf`; `easy-install`, not `easy-install.py`; etc. This also means you can rewrite your scripts in different languages without needing to modify every program that calls them to use a different name. – Charles Duffy Jul 18 '18 at 16:25
  • Thank you. I'll try and implement your comments now and I'll let you know how I get on – Paul Jul 18 '18 at 16:29
  • BTW, our Markdown here isn't GitHub-flavored -- triple backticks aren't special. If you want content to be treated as code (with syntax highlighting and the like), use four-space indents, or select a block and click the `{}` button. – Charles Duffy Jul 18 '18 at 16:29
  • By the way, is there a way to actually view the progress of the command being executed? – Paul Jul 18 '18 at 16:30
  • If you just want to see the commands as they execute, make it `#!/bin/bash -x`, or run `bash -x yourscript`; you can also put the command `set -x` inside. – Charles Duffy Jul 18 '18 at 16:30
  • BTW, I'd suggest putting `|| exit` after at least the `cd` command, so the script doesn't try to run in the wrong directory if the `cd` fails. (There's also a `set -e` option that tries to do something similar, but it has [serious problems](http://mywiki.wooledge.org/BashFAQ/105) and is rightly controversial; see the exercises after the allegory if in a hurry). – Charles Duffy Jul 18 '18 at 16:31
  • So you mean it would be something like: yarn run build -- --release set -x Sorry I know these may be stupid questions – Paul Jul 18 '18 at 16:32
  • rather, first line `#!/bin/bash`, second line `set -x`, third line `cd /var/www/website || exit` -- every shell command after the `set -x` line will print a log line when it starts. If you want to make `yarn` log its operations, that's going to be a specific question about `yarn`; the only generic approaches that work for all commands are syscall-level, and they log so many details as to be very difficult to follow, and can have a serious performance penalty. If that's what you want, though, look into [`sysdig`](http://www.sysdig.org/) (modern, fast, needs root) or `strace` (slow, no root). – Charles Duffy Jul 18 '18 at 16:33

0 Answers0