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/ /opt/wildfly-10.1.0.Final/bin/ ├── standalone.sh ... 1 directory, 49 files user@hostname:/$
The shell script is ‘standalone.sh’ 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/standalone.sh -b=0.0.0.0
Below is the output of the executed command above :
user@hostname:~$ sh /opt/wildfly-10.1.0.Final/bin/standalone.sh -b=0.0.0.0 ======================================================================= 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 -Djava.net.preferIPv4Stack=true -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 [org.jboss.as] (MSC service thread 1-7) WFLYSRV0049: WildFly Full 10.1.0.Final (WildFly Core 2.2.0.Final) starting 10:32:11,495 INFO [org.jboss.as.server] (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 [org.jboss.as.txn] (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 [org.jboss.as.jsf] (ServerService Thread Pool -- 44) WFLYJSF0007: Activated the following JSF Implementations: [main] 10:32:12,282 INFO [org.jboss.as.clustering.infinispan] (ServerService Thread Pool -- 38) WFLYCLINF0001: Activating Infinispan subsystem. 10:32:12,283 INFO [org.jboss.as.naming] (ServerService Thread Pool -- 46) WFLYNAM0001: Activating Naming Subsystem 10:32:12,344 INFO [org.jboss.as.security] (ServerService Thread Pool -- 53) WFLYSEC0002: Activating Security Subsystem 10:32:12,351 INFO [org.wildfly.extension.io] (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 [org.jboss.as.webservices] (ServerService Thread Pool -- 56) WFLYWS0002: Activating WebServices Extension 10:32:12,461 INFO [org.jboss.as.security] (MSC service thread 1-6) WFLYSEC0001: Current PicketBox version=4.9.6.Final 10:32:12,486 INFO [org.jboss.as.connector] (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 [org.jboss.as.connector.subsystems.datasources] (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/standalone.sh -b=0.0.0.0 & [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”