Showing posts with label linux. Show all posts
Showing posts with label linux. Show all posts

2024-07-03

Logrotate failed because parent directory has insecure permissions (It's world writable or writable by group which is not "root")

 

Error:

error: skipping "output.log" because parent directory has insecure permissions (It's world writable or writable by group which is not "root") Set "su" directive in config file to tell logrotate which user/group should be used for rotation.



Googled: Set "su" directive in config file to tell logrotate which user/group should be used for rotation.


Reference URL: https://www.suse.com/support/kb/doc/?id=000017263

su user group

in logrotate config file




/var/log/tomcat5/base/*.txt {
    su tomcat tomcat
    notifempty
    copytruncate
    weekly
    rotate 52
    compress
    missingok
}

/var/log/tomcat5/base/catalina.out {
    su tomcat tomcat
    notifempty
    copytruncate
    weekly
    rotate 52
    compress
    missingok
}


2024-05-23

Systemctl add new service.

 

Create the background service file


[Unit] Description=my-service nginx example # You may want to start after your network is ready After=network-online.target Wants=network-online.target [Service] ExecStart=/usr/bin/env docker run --name nginx --rm -p 8080:80 docker.io/nginx:alpine Restart=Always PIDFile=/tmp/my_service_pid [Install] WantedBy=network-online.target



How to create a systemd service


# Create the service file vim /etc/systemd/system/my-service.service # (if you change an existent file, you must reload the daemon config) # Reload daemon config systemctl daemon-reload # Enable systemctl enable my-service.service # Start systemctl start my-service.service # (if you need to restart) # Restart systemctl restart my-service.service









multi-user.target.wants



/etc/systemd/system/multi-user.target.wants/

You can see some link files of unit services and some directories of the “wants” of a target. For example, what the multi-user target wants to be loaded when the boot procedure reaches its level, is listed in the directory with name /etc/systemd/system/multi-user.target.wants/.




As you can see it doesn’t contain only services but also other targets which are also collections of services.

Let’s make a service unit with the name connection.service.

# vim connection.service

and type the following (hit “i” for insert mode), save it, and exit (with “esc” and “:wq!” ) :

[Unit]
Description = making network connection up
After = network.target

[Service]
ExecStart = /root/scripts/conup.sh

[Install]
WantedBy = multi-user.target





2022-04-07

Regex to split htop details

 

^( *\d+) (\w+) +(\d+) +(-?\d+) +(\d+.?\d?\w?) +(\w+) +(\w+) +S +(\d+.?\d+) +(\d+.?\d+) +(\d+h?:?\d?\d?:?\.?\d?\d?) +[├│└][\+─ ] (.*)$


2021-11-16

gzip with -9 option

 
A log file: with size of: 789M

gzip with different option, to have different compression as below

gzip -9 file.log
32M


gzip -9 file.log
37MB




example #2


-rw-r--r--. 1 srvusr srvusr  87M Jul 18 19:53 output.log.20220718_pm074234.log
-rw-r--r--. 1 root   root    87M Jul 21 12:01 output.log.20220718_pm074234.without9.log


sudo gzip -9 output.log.20220718_pm074234.log
sudo gzip output.log.20220718_pm074234.without9.log


-rw-r--r--. 1 srvusr srvusr 4.0M Jul 18 19:53 output.log.20220718_pm074234.log.gz
-rw-r--r--. 1 root   root   4.2M Jul 21 12:01 output.log.20220718_pm074234.without9.log.gz





2019-08-16

Sequence of 2>&1 in sh start script cause exceptions were not log into file, but show on screen.



Content of Hello.java


public class Hello
{
    public static void main(String[] args) throws Exception
    {
        System.out.println("Hello");
        throw new Exception("world");
    }
}






-------------------------------------------------------------------------------------------------------



[billson@virtual-jenkins Documents]$ javac Hello.java; java -cp . Hello 2>&1 > null.txt
Exception in thread "main" java.lang.Exception: world
    at Hello.main(Hello.java:8)


[billson@virtual-jenkins Documents]$ cat null.txt
Hello





-------------------------------------------------------------------------------------------------------

[billson@virtual-jenkins Documents]$ javac Hello.java; java -cp . Hello > null 2>&1


[billson@virtual-jenkins Documents]$ cat null.txt
Hello
Exception in thread "main" java.lang.Exception: world
    at Hello.main(Hello.java:8)








2019-08-15

Centos add user sudoers list





      1. Create more users
Eg:
sudo useradd weiyang
sudo passwd weiyang

With this user will be able to run sudo:
sudo usermod -aG wheel weiyang


[billson@169 home]$ groups weiyang
weiyang : weiyang wheel




2019-07-04

How to display number of items in sub-directories from command line



Reference URL:
https://askubuntu.com/questions/1150749/how-to-display-number-of-items-in-sub-directories-from-command-line/1150753#1150753



Here's a little shell function that you can use. Just add these lines to your ~/.bashrc:
lsc(){
    ## globs that don't match should expand to a null string
  shopt -s nullglob
  ## If no arguments were given use the current dir
  if [[ $# -eq 0 ]]; then
    targets="."
  ## Otherwise, use whatever was given
  else
    targets=($@)
  fi
  ## iterate over the arguments given
  for target in "${targets[@]}"; do
    ## get the contents of the target
    contents=("$target"/*)
    ## iterate over the contents
    for thing in "${contents[@]}";  do
      ## If this one is a directory
      if [[ -d "$thing" ]]; then
        ## collect the directory's contents
        count=("$thing"/*)
        ## Print the dir's name (with a '/' at the end)
        ## and the number of items found in it
        printf "%s/ (%s)\n" "$thing" "${#count[@]}"
      else
        ## If this isn't a dir, just print the name
        printf "%s\n" "$thing"
      fi
    done
  done
}
then open a new terminal and run:
lsc /path/to/dir
For example, given the following directory (the \012 are newlines in the names):












2019-06-26

crontab job report error/command not found, only error at schedule, not in manual execute sh script.






Reference URL: https://stackoverflow.com/questions/8127433/unable-to-run-a-service-command-via-cron






sbin is not in the path when run via cron. Specify the full path to service. This is probably either /sbin/service or /usr/sbin/service. You can find the path on your system by running which service.





Work around:


add " 2>&1" behind your crontab for those scripts you totally no idea how to handle "properly" in short time.








2019-04-09

ps -ef grep -v multi


Google'ed by: ps -ef grep -v multi




Reference URL: https://stackoverflow.com/a/13330924/676104


Use a separated list of of processes:
#!/bin/bash
PROC="nginx mysql ..."
for p in $PROC
do
  ps cax | grep $p > /dev/null

  if [ $? -eq 0 ]; then
    echo "Process $p is running."
  else
    echo "Process $p is not running."
  fi

done
If you simply want to see if either one of them is running, then you don't need loo. Just give the list to grep:
ps cax | grep -E "Nginx|mysql|etc" > /dev/null



To exclude by using:


ps -ef | grep -vE "grep|vim |tail " | grep tomcat








2019-04-01

Most common init systems for CentOS/others.


Reference URL: https://www.digitalocean.com/community/tutorials/how-to-configure-a-linux-service-to-start-automatically-after-a-crash-or-reboot-part-1-practical-examples


PostedAugust 19, 2015



  • System V is the older init system:
    • Debian 6 and earlier
    • Ubuntu 9.04 and earlier
    • CentOS 5 and earlier
  • Upstart:
    • Ubuntu 9.10 to Ubuntu 14.10, including Ubuntu 14.04
    • CentOS 6
  • systemd is the init system for the most recent distributions featured here:
    • Debian 7 and Debian 8
    • Ubuntu 15.04 and newer
    • CentOS 7




















2019-03-19

WinSCP configure hostkey in winscp script


What's WinSCP hostkey


Before OpenSSH 6.8
 ssh-keygen -lf <(ssh-keyscan localhost 2>/dev/null)



Since OpenSSH 6.8, you have to add the -E md5 switch to get the format needed for WinSCP.

ssh-keygen -E md5 -lf <(ssh-keyscan localhost 2>/dev/null)


[billson@169 ~]$ ssh-keygen -E md5 -lf <(ssh-keyscan localhost 2>/dev/null)
2048 MD5:aa:bb:cc:xx:xx:xx:xx:43:79:ae:90:ca:83:d3:xx:ef localhost (RSA)
256 MD5:xx:81:xx:7a:xx:49:68:87:xx:a3:54:xx:61:b3:xx:15 localhost (ECDSA)
256 MD5:xx:69:29:4a:xx:f0:8f:74:xx:5d:1b:86:aa:xx:d7:1b localhost (ED25519)



USING (RSA)
GET CONTENT BETTWEN [MD5:]*********************[localhost (RSA)] without space
SUPPOSE also using 2048


Replace
open sftp://user:password@example.com/ -hostkey="ssh-rsa 2048 xx:xx:xx:xx:xx:xx:xx:xx..."
to:
open sftp://user:password@example.com/ -hostkey="ssh-rsa 2048 aa:bb:cc:xx:xx:xx:xx:43:79:ae:90:ca:83:d3:xx:ef"




Google Referrals