Run Shell Script in Background Process via Command Line

Posted on

The article in this occasion is written on how to run shell script in background process via command line. Basically the example of the shell script run in the context of this article is a Wildfly Java Application Server shell script. The shell script is normally ended with the file extension of .sh and as the example given in this article, the shell script is located in the folder of /opt/wildfly-10.1.0-Final/bin :

user@hostname:/$ tree /opt/wildfly-10.1.0.Final/bin/

1 directory, 49 files

The shell script is ‘’ and it must be executed without any chances to be terminated accidentally after the script itself is executed. For an example, below is the normal execution of the shell script :

sh /opt/wildfly-10.1.0.Final/bin/ -b=

Below is the output of the executed command above :

user@hostname:~$ sh /opt/wildfly-10.1.0.Final/bin/ -b=

JBoss Bootstrap Environment

JBOSS_HOME: /opt/wildfly-10.1.0.Final

JAVA: /opt/java/jdk/jdk1.8.0_05/bin/java

JAVA_OPTS: -server -Xms64m -Xmx512m -XX:MetaspaceSize=96M -XX:MaxMetaspaceSize=256m -Djboss.modules.system.pkgs=org.jboss.byteman -Djava.awt.headless=true


10:32:06,835 INFO [org.jboss.modules] (main) JBoss Modules version 1.5.2.Final
10:32:07,472 INFO [org.jboss.msc] (main) JBoss MSC version 1.2.6.Final
10:32:07,642 INFO [] (MSC service thread 1-7) WFLYSRV0049: WildFly Full 10.1.0.Final (WildFly Core 2.2.0.Final) starting
10:32:11,495 INFO [] (Controller Boot Thread) WFLYSRV0039: Creating http management service using socket-binding (management-http)
10:32:11,656 INFO [org.xnio] (MSC service thread 1-5) XNIO version 3.4.0.Final
10:32:11,696 INFO [org.xnio.nio] (MSC service thread 1-5) XNIO NIO Implementation Version 3.4.0.Final
10:32:12,156 WARN [] (ServerService Thread Pool -- 54) WFLYTX0013: Node identifier property is set to the default value. Please make sure it is unique.
10:32:12,169 INFO [] (ServerService Thread Pool -- 44) WFLYJSF0007: Activated the following JSF Implementations: [main]
10:32:12,282 INFO [] (ServerService Thread Pool -- 38) WFLYCLINF0001: Activating Infinispan subsystem.
10:32:12,283 INFO [] (ServerService Thread Pool -- 46) WFLYNAM0001: Activating Naming Subsystem
10:32:12,344 INFO [] (ServerService Thread Pool -- 53) WFLYSEC0002: Activating Security Subsystem
10:32:12,351 INFO [] (ServerService Thread Pool -- 37) WFLYIO001: Worker 'default' has auto-configured to 8 core threads with 64 task threads based on your 4 available processors
10:32:12,364 INFO [] (ServerService Thread Pool -- 56) WFLYWS0002: Activating WebServices Extension
10:32:12,461 INFO [] (MSC service thread 1-6) WFLYSEC0001: Current PicketBox version=4.9.6.Final
10:32:12,486 INFO [] (MSC service thread 1-2) WFLYJCA0009: Starting JCA Subsystem (WildFly/IronJacamar 1.3.4.Final)
10:32:12,936 INFO [org.jboss.remoting] (MSC service thread 1-5) JBoss Remoting version 4.0.21.Final
10:32:12,999 INFO [org.wildfly.extension.undertow] (MSC service thread 1-6) WFLYUT0003: Undertow 1.4.0.Final starting
10:32:13,632 INFO [] (ServerService Thread Pool -- 33) WFLYJCA0004: Deploying JDBC-compliant driver class org.h2.Driver (version 1.3)

The main problem of the above command execution is the window of the command must be active as long as the service of the Wildfy Java Application Server is going to be preserved. If the window is accidentally closed or whenever the service is started in a remote server and suddenly the connection to that remote server is off, it will terminate the service. So, in order to preserve and to pertain the service although the window is being closed or even the connection to a remote server where the service is started suddenly cut-off, the above command needs to be modified. Based on the experience on executing the command, adding the ‘ampersand’ (&) sign in the end of the command will also terminate the process or the service if the connection to the remote server where the service is started. To be able to preserve the service, below is the command execution which is modified :

[root@mailrelay ~]# nohup sh /opt/wildfly-10.1.0.Final/bin/ -b= &
[1] 22897
[root@mailrelay ~]# nohup: ignoring input and appending output to ‘nohup.out’

So, the pattern is shown below :

nohup sh shell_script_file_name_and_location parameter &

One thought on “Run Shell Script in Background Process via Command Line

Leave a Reply