Some examples to use custom scripts triggered on server and node events (NoMachine 4 or later)

Added on: 2014-02-28 Last Modified: 2019-10-17
ID: AR02L00787 Applies To: NoMachine Server

Examples below applies to NoMachine 4 or later.

Example 1:

Use a custom script to run a specific application inside the session, e.g. Firefox

1.  Create the runApplication.sh script. The script must be executable.

2. Place the script in a directory that is accessible by the node, i.e. accessible by the connecting user since the node runs as such user.

3. The script should be similar to:

#!/bin/sh

#variables
userName=$2
displayNumber=$4

export HOME=/home/$userName
export DISPLAY=:$displayNumber
export PATH="/bin:/usr/bin"

firefox 0<&- &>/dev/null &

exit 0

 

4. In the node configuration file, namely /usr/NX/etc/node.cfg on Linux set the following key:

UserScriptAfterSessionStart = "/usr/NX/scripts/custom/runApplication.sh"

 

Example 2:

Use custom scripts to run a dialog and provide hostname and display info to the end-user

If you want to provide additional information to the end-user you can create a custom script for issuing dialog messages via the NoMachine client.

Example below explains how to show a dialog message providing information about hostname and display. This script will be executed when a new session is started and when a virtual desktop is connected in 'shadow' mode. It works also for custom sessions. In a multi-node environment, you have to apply the custom script on each node.


1.  Create the displayMessageStart.sh script. The script must be executable.

2. Place the script in a directory that is accessible by the node, i.e. accessible by the connecting user since the node runs as such user.

3. The script should be similar to:

#!/bin/sh

sessionID=$1
userName=$2
sessionType=$3
displayNumber=$4
host=`hostname`
message="$userName@$host:$displayNumber"

export HOME=/home/$userName

if [ "$sessionType" == "virtualAttach" ] ; then
  sessionPath=$(find /usr/NX/var/log/node/ -regextype posix-egrep -type d -regex /usr/NX/var/log/node/C-.*$sessionID 2>/dev/null)

  export XAUTHORITY="$sessionPath/authority"

  displayNumber=$(grep "shadow=" $sessionPath/options | sed 's/.*shadow=:(.*):.*/1/')
fi

/usr/NX/bin/nxclient --dialog 'ok' --message "$message" --display :$displayNumber --class 'info' --allowmultiple > /dev/null 2>&1 &

exit 0

4. In the node configuration file, namely /usr/NX/etc/node.cfg on Linux set the following key:

UserScriptAfterSessionStart = "/usr/NX/scripts/custom/displayMessageStart.sh"
 

Example 3:

Use custom scripts to run a dialog showing an error message if the remote application fails to run

This example is suitable for custom sessions, but can be used also for other session types. Stderr and Stdout of X client applications is redrected to the clients file log in the session directory created in /usr/NX/var/log/node. This script retrieve possible errors from that file and show them in a dialog displayed to the end-user.

To set-up this custom script, follow the same instructions provided for Example 2, but change the script at point 3 to the example below.

 

#!/bin/sh

sessionID=$1
userName=$2
sessionType=$3
displayNumber=$4
host=`hostname`
message="$userName@$host:$displayNumber"

export HOME=/home/$userName

if [ "$sessionType" == "virtualAttach" ] ; then
 sessionPath=$(find /usr/NX/var/log/node/ -regextype posix-egrep -type d -regex /usr/NX/var/log/node/C-.*$sessionID 2>/dev/null)

 export XAUTHORITY="$sessionPath/authority"

 displayNumber=$(grep "shadow=" $sessionPath/options | sed 's/.*shadow=:(.*):.*/1/')
fi

appOutput=$(cat "$NX_SYSTEM/var/log/node/C-$host-$displayNumber-$sessionID/clients")
/usr/NX/bin/nxclient --dialog 'ok' --message "$appOutput" --display :$displayNumber --class 'info' --allowmultiple > /dev/null 2>&1 &

exit 0