0

I am new to linux environment and trying to write few services and want to start each of them via single shell script but not able to do so.

I have few linux executable placed under subfolders of application. It is like:

MainApp
  AppAFolder
    App-A
  AppBFolder
    App-B
startServices.sh

I want to run each of the applications present in sub-folders in background forever(even after main terminal is closed).

When I try below commands separately in Linux terminal, it starts the services.

cd AppAFolder && chmod +x App-A && ./App-A &>/dev/null & disown

or

cd AppBFolder && chmod +x App-B && ./App-B &>/dev/null & disown

But, when i place same commands inside shell script and try to execute, it seems to be changing directory and second service does not run.

#!/bin/bash
echo "Starting ServiceA. Currect dir is : $PWD"
ACmd="cd AppAFolder && chmod +x App-A && ./App-A &>/dev/null & disown"
$ACmd

echo "Starting ServiceB. Currect dir is : $PWD"
BCmd="cd AppBFolder && chmod +x App-B && ./App-B &>/dev/null & disown"
$BCmd

I get error "./startServices.sh: line 12: cd: AppBFolder: No such file or directory".

I want to spwan each service in new terminal in background and detach from main process so that it can run forever untill stopped.

Please let me know what I am missing here.

Thanks, Pooja

pooja
  • 319
  • 1
  • 2
  • 21
  • can it be that you have to cd back to the start directory after doing $Acmd? – ruud May 20 '20 at 11:40
  • 1
    and don't forget to start the command with nohup. If you don't, the processes will terminate after the terminal is closed. – ruud May 20 '20 at 11:59
  • Are you sure you need to use cd ? Can't you simply use `chmod +x ./AppAFolder/App-A && ./AppAFolder/App-A &>/dev/null & disown` ? – Aserre May 20 '20 at 12:06
  • @Aserre, I tried that. But each of the services try to access files using relative path from current working directory, So doing this, fails to load those files and gives error. – pooja May 21 '20 at 04:32

2 Answers2

4

The man page for disown says you can use nohup for the same purpose. I would recommend that because it is more specific about which command is affected. You can make a cd command affect only some commands by grouping them under a "subshell". Putting all of that together yields:

#!/bin/bash
echo "Starting ServiceA. Currect dir is : $PWD"
(cd AppAFolder && chmod +x App-A && nohup ./App-A &>/dev/null &)

echo "Starting ServiceB. Currect dir is : $PWD"
(cd AppBFolder && chmod +x App-B && nohup ./App-B &>/dev/null &)
Eric Bolinger
  • 2,722
  • 1
  • 13
  • 22
  • I agree that this is the answer, but `command & disown $!` is better than `nohup` in my opinion because it doesn't leave around nohup.out files. I've also use double-forking for the same effect: `(command&)&` – rand'Chris May 20 '20 at 17:25
  • Agreed. But the OP already redirected stderr and stdout, which prevents the nohup.out crud. So we're good! – Eric Bolinger May 20 '20 at 17:36
  • Thanks @EricBolinger. got that working with the suggestion. Wandering if we do have command to stop services in linux? I am using Stop-Process currently for windows to stop. I checked and found killall [SERVICE_NAME] – pooja May 21 '20 at 05:15
  • Stopping background processes gets tricky -- depends on the OS and shell. Go find other posts about it, like: https://stackoverflow.com/q/360201/3121039 – Eric Bolinger May 21 '20 at 16:39
  • @pooja The modern way to handle this is with systemd unit files. These can be run as user services as well. Maybe this is enough to get you started: https://unix.stackexchange.com/questions/200654/executing-chdir-before-starting-systemd-service – rand'Chris May 21 '20 at 18:46
0

When you run your bash script, the first command will change directory to AppAFolder. When you run the second command in the bash script, it is still in the AppAFolder and AppBfolder does not exist in the folder. You probably need to move up a folder first before running the second command.

Paul
  • 264
  • 3
  • 9