Stopping a command launched with the rest api

Hi to all !
I’m developing a small application (pure javascript+html+css) to help administrate our containers.
I’ve found out how to execute a command via the REST api. This part works fine.
Here are the parameters I POST to https://lxd-server:8443/1.0/containers/container/exec :

var params={“command”: [“tail”, “-f”, ‘/var/log/apache2/access.log’], “record_output”: false, “wait-for-websocket”: true, “interactive”: true, “environment”: { “HOME”: “/root”, “TERM”: “xterm-256color”, “USER”:“root”, “SYSTEMD_PAGER”: ‘’}, “width”: 192, “height”: 48};

As one can read, the command is a “tail -f /var/log/apache2/access.log”.
After this is POSTed to the convenient url, I get the file descriptors and operate mainly on stdin through a websocket opened on the correct url.
This again works fine and I have the display behave correctly.

This said, what would the best way to stop the so-launched remote command ?
Send a DELETE on the operation ?
Destroy the websocket (I’m not sure this would do anything but kill the display process) ?
Some other thing I can’t think of ?

Thanks to anyone who can help me or direct me towards some solution to my problem.
Regards.

The control websocket allows for sending signals to the process, you can use that to send a SIGTERM or SIGKILL.

A DELETE of the operation won’t work as I don’t believe this particular operation has a canceler setup.

Alternative would be to start another command which kills the tail command from inside the container, but it feels like using the control socket is likely the best option.

Thank you so much for your quick and acurate answer @stgraber

I’ll give a try to using the control websocket. This one is already opened in my code, but I haven’t figured out how to use it.

From what I’ve read over here :

I should write to the websocket something like ‘{“command”: “signal”, “signal”: 15}’… I’ll give it a try :slight_smile:

I’ve had time to give it a try :
wsControl.send (’{“command”: “signal”,“signal”:15}’)

does the job, well at least it stops the command. However, there is no feedback that the operation is stopped.

I have another websocket opened on /events?type=operation
and another one opened on /events?type=lifecycle

None of them get informed that the operation launched (/1.0/containers/container/exec) was stopped with the signal.

Is that a normal and expected behavior ?

I got it !

For memories’ purpose and if someone else has the same needs, here is how I solved this as I expected :

  • first post the commands to the url : 1.0/containers/container/exec
  • then get back the websockets url and open them all (control, stdin at least)
  • when you want to terminate the command :
  1. send the signal (2) to the control websocket
  2. close the control websocket
  3. close the stdin websocket

Then, there is the feedback I expected on the overall websocket that lists the ongoing operations.

1 Like