1

I'm using a keycloak server, when I run this command:

standalone.sh 

This command launchs the server and I'm not able to stop it until I execute Ctrl-C command. I though about runing an instruction like this:

standalone.sh && jboss-cli.sh -c --commands=shutdown

or

standalone.sh ; jboss-cli.sh -c --commands=shutdown

Based on this question What are the shell's control and redirection operators? I found that ;

Will run one command after another has finished, irrespective of the outcome of the first.

And &&

Used to build AND lists, it allows you to run one command only if another exited successfully.

But in my case the first task did not exit and still executing. Is there any solution to run another task which will stop the first?

Kusalananda
  • 333,661
Slim
  • 113
  • Does the second command stop the first? When would you want to run that second command? After a certain time? – Kusalananda Mar 05 '18 at 13:24
  • @Kusalananda yes the second will stop the first after the first will be executed – Slim Mar 05 '18 at 13:25
  • But the first one does not finish? I'm assuming it's running something in the foreground that prevent it from actually exiting and giving back control to you script. What does the first script look like? – Kusalananda Mar 05 '18 at 13:26
  • Actually the first instruction is the script which launchs a server after that the server still in execution so I'm trying to execute the second instruction to stop it. – Slim Mar 05 '18 at 13:28
  • If I run the first in a single instance and run the second in another terminal instance. The server stops however using the two instructions in the same line did not fix the problem. – Slim Mar 05 '18 at 13:30
  • 1
    Are you just looking for &? Running standalone.sh & will run it in the background, so you will still have access to the terminal and can run jboss-cli.sh -c --commands=shutdown whenever you want. – terdon Mar 05 '18 at 13:31
  • 1
    This question is unclear: It is unclear whether you want to start the keycloak server with the first script and the immediately shut it down with the second command, or whether the first script is supposed to start the server, do some things, and then the server should be shut down. We don't know what your first script is doing or what it needs to do. – Kusalananda Mar 05 '18 at 14:27

2 Answers2

1

Add a single & at the end of the first command.

standalone.sh & jboss-cli.sh -c --commands=shutdown

That single ampersand is a command delimiter, just like ;, but sends the prior command to run asynchronously, in the background, and lets the following command(s) start immediately.

user1404316
  • 3,078
  • 1
    This will also stop the first task immediately, which may not be wanted (?) – Kusalananda Mar 05 '18 at 13:48
  • @Kusalananda - no, it should not. From man bash: "If a command is terminated by the control operator &, the shell executes the command in the background in a subshell. The shell does not wait for the command to finish, and the return status is 0. Try, for instance xclock -digital -update 1 & xclock -digital -update 1 (The second instance may overlay the first, so you may need to move the top window) – user1404316 Mar 05 '18 at 14:11
  • @Kusalananda - you may be confusing the ampersand delimiter with the C-z "suspend" keybinding. – user1404316 Mar 05 '18 at 14:15
  • 1
    What I mean is that if I'm reading the question correctly, the first script will start some servire, and the second command will shut it down. If you run start-service-script & shutdown-service-command then the service will not be running for very long. – Kusalananda Mar 05 '18 at 14:24
  • @Kusalananda - yes, that is exactly what the OP is indicating in his example, which I reflected back to him/her. Your comment is probably best directed at the OP, so we can both know for sure the intent, but I suspect it was just presented as an example. – user1404316 Mar 05 '18 at 14:30
1

As noted by user1404316, standalone.sh & jboss-cli.sh -c --commands=shutdown is probably what you want. The only issue with that is that if the standalone.sh takes time to execute (which it probably does), I would insert a sleep to offset the execution time such as:

standalone.sh & sleep $amount_of_time_in_seconds; jboss-cli.sh -c --commands=shutdown

Jaken551
  • 565