How to run NoMachine Linux virtual desktops by command line
Since version 6.2, a new server's tool is available for starting virtual desktop sessions on Linux from command line. Such command line tool is intended to be run as non privileged user, the virtual desktop will be run for the user who executes the command. (Ref. https://www.nomachine.com/FR06O03458)
This feature can be used also for making benchmark tests useful to sizing the HW of the NoMachine server host machine or of the Terminal Server Node in case of a multi-node environment.
The server command to create virtual desktops is:
/etc/NX/nxserver --startsession --virtual [--type <type> ]
If --type is not provided, this command will start the desktop environment defined in the DefaultDesktopCommand key in the /usr/NX/etc/node.cfg file. It correspondes to '--type unix-default'.
The --type parameter can be used to specify which type of desktop should be launched:
nxserver --startsession --virtual --type unix-default
nxserver --startsession --virtual --type unix-gnome
nxserver --startsession --virtual unix-kde --type
To start the console application in virtual desktop mode:
nxserver --startsession --virtual unix-console --type
To start a custom application in virtual desktop mode:
nxserver --startsession --virtual --type unix-application --application <application name or path>
Some practical examples
Example 1
It's possible to use the 'nxserver --startsession' command to start a virtual desktop on user's behalf. E.g. to start the virtual desktop for user 'Adam':
su - Adam -c "nxserver --startsession --virtual --type unix-default"
This could be useful for example for pre-launching virtual desktops which can be then reconnected by the corresponding users or connected in shadow mode from other users.
Example 2
The following script is intended to be an example about how to use the new command. It could be used also for benchmark tests. The maximum number of virtual desktops that can be created is limited by the license type (e.g. Workstation allows up to four virtual desktops, while Terminal Server has unlimited virtual desktops).
This script, named sessionstart.sh, takes (i) the number of sessions to be created and (ii) the session type in input.
For example, to create three GNOME virtual desktops execute the script with:
sudo ./sessionstart.sh 3 unix-gnome
Start session number 1.
------------------------
NX> 152 Session started on display '1002'.
INFO: New session used 794 MB ram.
INFO: Free RAM left: 28384 MB.
INFO: New session started in 2408 miliseconds.
Start session number 2.
------------------------
NX> 152 Session started on display '1003'.
INFO: New session used 873 MB ram.
INFO: Free RAM left: 27511 MB.
INFO: New session started in 2481 miliseconds.
Start session number 3.
------------------------
NX> 152 Session started on display '1004'.
INFO: New session used 874 MB ram.
INFO: Free RAM left: 26637 MB.
INFO: New session started in 2439 miliseconds.
The script, sessionstart.sh:
#!/bin/bash
# This script creates multiple virtual desktop sessions using the
# nxserver --startsession --virtual command.
#
# Usage:
# ./startsession.sh
#
# E.g. to create 10 unix-gnome session, run:
# ./startsession 10 unix-gnome
#
# or to create 10 Firefox custom sessions running in a virtual desktop:
# ./startsession 10 unix-application --application firefox
#
# Do not execute this script as sudo user or root, the nxserver --startsession
# command should be executed by an unprivileged user.
session_number=$1
shift
session_type="$@"
# wait some time after session start to initialise all session processes
wait_time="20"
for i in $(seq 1 $session_number) ; do
echo "Start session number $i."
echo "------------------------"
# Check RAM usage and time before session start
ram_before="$( free -m | grep Mem: | awk ' print $4 ' )"
time_before="$( date +%N )"
# Start session with type defined as script argument
/etc/NX/nxserver --startsession --virtual --type $session_type
# Check RAM usage and time after session start
time_after="$( date +%N )"
sleep $wait_time
ram_after="$( free -m | grep Mem: | awk ' print $4 ' )"
# Display information after session start
echo -e "\nINFO: New session used $(( $ram_before - $ram_after )) MB ram."
echo "INFO: Free RAM left: $ram_after MB."
echo "INFO: New session started in $((( time_after - time_before) /1000000 )) miliseconds."
echo ""
done
Some notes to the script:
1) Time required to start the session is related to the time necessary to the server to launch the system command for creating the desktop or the application. The full display of the session may require longer, depending on time requested by the desktop to initialize all its processes.
2) The script introduces a sleep ( wait_time="20") necessary for checking RAM usage. It ensure to wait until the session is start-up.
3) The nxserver --startsession command always exits with 0, regardless if the session has been created or not.
