Execute Shell Command from WebVI

Updated Sep 20, 2021

Environment

Software

  • G Web Development Software
  • LabVIEW
  • LabVIEW NXG Web Module

It is a known fact that webVI cannot execute shell commands. This is expected behavior but it cannot be denied that in some situations, you need to perform a specific action that can only be done by executing specific shell command such as start streaming service of VLC Media Player.

This article will demonstrate how you can utilize web service from NI Web Server to enable the execution of shell commands from webVI.

The working principle of this approach is having the webVI sends HTTP requests to Web Service hosting by NI Web Server, to execute the shell command. Instructions below are the steps needed to set up the Web Service.
  1. Create a LabVIEW project.
  2. Right-click My Computer and select New to create a Web Service
    Create Web Service
  3. Assign the Web Service a suitable name.
  4. Expand Web Service and right-click Web Resources to create a VI for it. 
    web resources.
  5. Right-click the newly created VI and select GET as its HTTP request method.  
    HTTP request
  6. Double-click the newly created VI and there should be an object known as LabVIEW Web Service Request in your block diagram.
  7. Insert the following snippet into your block diagram. The following snippet will open Microsoft Notepad by using the command prompt.
    cmd
    Note: This image is a LabVIEW snippet, which includes LabVIEW code that you can reuse in your project. To use a snippet, right-click the image, save it to your computer, and drag the file onto your LabVIEW diagram.
  8. Save the VI and your LabVIEW project.
  9. Right-click your Web Service and select Start
    start
  10. Right-click your VI and select Show Method URL to get the URL that helps trigger this VI. 
    url
  11. Copy the URL and paste it into your web browser's search box and press Enter.
  12. Microsoft Notepad will be launched automatically once you access the URL.
You can now utilize LabVIEW NXG Web Module or G Web Development Software's HTTP API to access the URL and execute specific shell commands.