ubuntu cli / scripting

main
Yik Teng Hie 5 years ago
parent ad1fe857cc
commit 90a8eb8260

@ -0,0 +1,59 @@
# shell Programming
* [Tutorial](https://www.tutorialspoint.com/unix/shell_scripting.htm)
```sh
#!/bin/sh
echo "File Name: $0"
echo "First Parameter : $1"
echo "Second Parameter : $2"
echo "Quoted Values: $@"
echo "Quoted Values: $*"
echo "Total Number of Parameters : $#"
```
* while loop
```sh
#!/bin/sh
a=0
while [ "$a" -lt 10 ] # this is loop1
do
b="$a"
while [ "$b" -ge 0 ] # this is loop2
do
echo -n "$b "
b=`expr $b - 1`
done
echo
a=`expr $a + 1`
done
```
* Assume variable **a** holds 10 and variable **b** holds 20 then
| Operator | Description | Example |
| :------: | :----------------------------------------------------------: | :------------------------: |
| **-eq** | Checks if the value of two operands are equal or not; if yes, then the condition becomes true. | [ $a -eq $b ] is not true. |
| **-ne** | Checks if the value of two operands are equal or not; if values are not equal, then the condition becomes true. | [ $a -ne $b ] is true. |
| **-gt** | Checks if the value of left operand is greater than the value of right operand; if yes, then the condition becomes true. | [ $a -gt $b ] is not true. |
| **-lt** | Checks if the value of left operand is less than the value of right operand; if yes, then the condition becomes true. | [ $a -lt $b ] is true. |
| **-ge** | Checks if the value of left operand is greater than or equal to the value of right operand; if yes, then the condition becomes true. | [ $a -ge $b ] is not true. |
| **-le** | Checks if the value of left operand is less than or equal to the value of right operand; if yes, then the condition becomes true. | [ $a -le $b ] is true. |
| Operator | Description | Example |
| :------: | :----------------------------------------------------------: | :-----------------------------------: |
| **!** | This is logical negation. This inverts a true condition into false and vice versa. | [ ! false ] is true. |
| **-o** | This is logical **OR**. If one of the operands is true, then the condition becomes true. | [ $a -lt 20 -o $b -gt 100 ] is true. |
| **-a** | This is logical **AND**. If both the operands are true, then the condition becomes true otherwise false. | [ $a -lt 20 -a $b -gt 100 ] is false. |
* Other [Operator](https://www.tutorialspoint.com/unix/unix-basic-operators.htm)

@ -0,0 +1,102 @@
# Quick Guide for auto start service
* [Reference](https://medium.com/@benmorel/creating-a-linux-service-with-systemd-611b5c8b91d6)
* [Reference 2](https://linuxconfig.org/how-to-create-systemd-service-unit-in-linux)
* [DZone](https://dzone.com/articles/run-your-java-application-as-a-service-on-ubuntu)
* create service script in `/etc/systemd/system/myserver.service`
```ini
[Unit]
Description=ROT13 demo service
After=network.target
StartLimitIntervalSec=0
[Service]
Type=simple
Restart=always
RestartSec=1
User=centos
ExecStart=/usr/bin/env php /path/to/server.php
[Install]
WantedBy=multi-user.target
```
* sample java application
```sh
[Unit]
Description=My Webapp Java REST Service
[Service]
User=ubuntu
# The configuration file application.properties should be here:
#change this to your workspace
WorkingDirectory=/home/ubuntu/workspace
#path to executable.
#executable is a bash script which calls jar file
ExecStart=/home/ubuntu/workspace/my-webapp
SuccessExitStatus=143
TimeoutStopSec=10
Restart=on-failure
RestartSec=5
[Install]
WantedBy=multi-user.target
```
* script `/home/ubuntu/workspace/my-webapp`. Change to executable `sudo chmod u+x my-webapp`
```sh
#!/bin/sh
sudo /usr/bin/java -jar my-webapp-1.0-SNAPSHOT.jar server config.yml
```
* start service
```sh
$ sudo systemctl start myserver.service
// or
$ sudo systemctl start myserver
```
* to restart
```sh
$ sudo systemctl restart myserver.service
```
* automatically start on boot
```sh
$ sudo systemctl enable myserver.service
```
* check active
```sh
$ systemctl is-active myserver.service
```
* All services status
```sh
$ service --status-all
```
Loading…
Cancel
Save