Skip to main content

How to autostart a Java jar after the OS has been restarted - Centos7/8

If you need to autostart a jar after your operative system has been rebooted using Centos 7/8, then you need to create a service, to do that go to /etc/systemd/system and create in there the name of the service you want to start your app, I gave in this example the service name as springboot.service
I am working with screen for the purpose of this example
before starting you need having screen installed, if you don't have it run yum install screen



so vim springboot.service and paste the next instructions in order to set up your service, replace the jar name and the WorkingDirectory by yours, the WorkingDirectory is usually where the jar is so that in most common cases in that directory is where your application.properties and other settings you've got



[Unit]
Description=JavaSpringBoot
After=network-online.target

[Service]
Type=forking
User=root
Group=root
WorkingDirectory=/home/centos/springboot_jwt/
ExecStart=/usr/bin/screen -S springboot -d -m /usr/bin/java -jar /home/centos/springboot_jwt/Generic-0.0.1-SNAPSHOT.jar
TimeoutSec=300

[Install]
WantedBy=multi-user.target

after you have created and save the service run:

systemctl daemon-reload with that command services are being updated and if there is a new one, it will automatically loaded by systemd

systemctl enable springboot.service this command is the one is going to create teh symlink in order to autostart it

systemctl start springboot.service it starts the service

systemctl status springboot.service it gives you the service status



now if you run screen -r -d you are going to be able to be reattached to the launched jar

you can run chance the user root by any other user and it is the best thing to do.


so now let's suppose you need to start a service only after the first jar has entered Active, let's name that service as springboot2.service



[Unit]
Description=JavaSpringBoot
After =springboot.service

[Service]
Type=forking
User=root
Group=root
WorkingDirectory=/home/centos/input_output/
ExecStartPre=/bin/sleep 15 
ExecStart=/usr/bin/screen -S springboot2 -d -m /usr/bin/java -jar /home/centos/input_output/InputOutputControlRegister-0.0.1-SNAPSHOT.jar
Restart=on-failureTimeoutSec=300

[Install]
WantedBy=multi-user.target


so the second service will be activated after the first service springboot.service
has started giving it 15 delay seconds, I am explaining this thing so that in my case the second jar needs from the first jar so that the first one is a service which provides some special data to the second jar, and if both of them started at the same time, the second jar could not start so that it depends from the first one to be up and running, and if you've got a kind of need like that just take this solution.

Comments

Popular posts from this blog

How to deploy a VueJS App using Nginx on Ubuntu

There are thousands of blogs and websites out there explaining how to do a hello world and how to start with VueJS, but in this little post, I’m just going to be explaining how to do deploy a VueJs app after you have run the command through the CLI npm run build . So when you run the command npm run build a dist folder is created and that folder’s got the essential .js files to run our app, when we want to run our app on an Nginx server by default the vue-router is not going to work well so that Nginx does not come ready to work by default with a VueJs app This is basically for a Linux Ubuntu distribution if you’ve got any other Linux distribution just pay attention where is going to be set the www/html folder and the Nginx settings so that this is useful for any Linux distribution  Install and configure nginx sudo apt install nginx Double check to make sure the nginx service is running with command service nginx status, then open your browser and enter url

How to do pagination SpringBoot with Jbctemplate, MySQL

We are going to be working on a topic which is a basic need when doing any app, and it is Pagination. let's get started creating a product table at https://mockaroo.com/ create table products ( id INT, name VARCHAR(50), code VARCHAR(50) ); insert into products (id, name, code) values (1, 'Hettinger-Goyette', '42549-680'); insert into products (id, name, code) values (2, 'Konopelski-Klein', '49527-724'); insert into products (id, name, code) values (3, 'Smitham, Kuhlman and Balistreri', '53238-003'); insert into products (id, name, code) values (4, 'Hettinger, Weissnat and Goodwin', '0143-9916'); insert into products (id, name, code) values (5, 'Rowe Inc', '42291-898'); insert into products (id, name, code) values (6, 'Ernser-Hauck', '10544-617'); insert into products (id, name, code) values (7, 'Maggio and Sons', '68788-9087'); insert into products (id, name,

How to secure SpringBoot with SSL and Tomcat or Undertow

when we are going to take our applications to production mode, we must have an SSL certificate for the FrontEnd and   BackEnd too, in this case, our backend is a  SpringBoot which is using a tomcat embedded server. Terminology TLS vs SSL TLS is the successor to SSL. It is a protocol that ensures privacy between communicating applications. Unless otherwise stated, in this document consider TLS and SSL as interchangable. Certificate (cert) The public half of a public/private key pair with some additional metadata about who issued it etc. It may be freely given to anyone. Private Key A private key can verify that its corresponding certificate/public key was used to encrypt data. It is never given out publicly. Certificate Authority (CA) A company that issues digital certificates. For SSL/TLS certificates, there are a small number of providers (e.g. Symantec/Versign/Thawte, Comodo, GoDaddy, LetsEncrypt) whose certificates are included by most browsers and Operating System