2022年6月13日星期一

linux stat file


$  stat /etc/passwd
  File: ‘/etc/passwd’
  Size: 1596            Blocks: 8          IO Block: 4096   regular file
Device: ca01h/51713d    Inode: 19293210    Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2022-06-03 09:21:07.905548960 +0900
Modify: 2022-06-03 09:21:07.909549008 +0900
Change: 2022-06-03 09:21:07.909549008 +0900
 Birth: -
$  stat -c "%Y" /etc/passwd   # time of last modification, seconds since Epoch
1654215667
stat --help
Usage: stat [OPTION]... FILE...
Display file or file system status.

Mandatory arguments to long options are mandatory for short options too.
  -L, --dereference     follow links
  -f, --file-system     display file system status instead of file status
  -c  --format=FORMAT   use the specified FORMAT instead of the default;
                          output a newline after each use of FORMAT
      --printf=FORMAT   like --format, but interpret backslash escapes,
                          and do not output a mandatory trailing newline;
                          if you want a newline, include \n in FORMAT
  -t, --terse           print the information in terse form
      --help     display this help and exit
      --version  output version information and exit

The valid format sequences for files (without --file-system):

  %a   access rights in octal
  %A   access rights in human readable form
  %b   number of blocks allocated (see %B)
  %B   the size in bytes of each block reported by %b
  %C   SELinux security context string
  %d   device number in decimal
  %D   device number in hex
  %f   raw mode in hex
  %F   file type
  %g   group ID of owner
  %G   group name of owner
  %h   number of hard links
  %i   inode number
  %m   mount point
  %n   file name
  %N   quoted file name with dereference if symbolic link
  %o   optimal I/O transfer size hint
  %s   total size, in bytes
  %t   major device type in hex, for character/block device special files
  %T   minor device type in hex, for character/block device special files
  %u   user ID of owner
  %U   user name of owner
  %w   time of file birth, human-readable; - if unknown
  %W   time of file birth, seconds since Epoch; 0 if unknown
  %x   time of last access, human-readable
  %X   time of last access, seconds since Epoch
  %y   time of last modification, human-readable
  %Y   time of last modification, seconds since Epoch
  %z   time of last change, human-readable
  %Z   time of last change, seconds since Epoch

Valid format sequences for file systems:

  %a   free blocks available to non-superuser
  %b   total data blocks in file system
  %c   total file nodes in file system
  %d   free file nodes in file system
  %f   free blocks in file system
  %i   file system ID in hex
  %l   maximum length of filenames
  %n   file name
  %s   block size (for faster transfers)
  %S   fundamental block size (for block counts)
  %t   file system type in hex
  %T   file system type in human readable form

NOTE: your shell may have its own version of stat, which usually supersedes
the version described here.  Please refer to your shell's documentation
for details about the options it supports.

GNU coreutils online help: <http://www.gnu.org/software/coreutils/>
For complete documentation, run: info coreutils 'stat invocation'

linux date format

$ date
Mon Jun 13 03:31:39 UTC 2022
# 現在の日時をUTCで表示
$ date -u
Mon Jun 13 03:35:28 UTC 2022
# format: ymd-hms
$ date "+%Y%m%d-%H%M%S"
20220613-033446
#formart: ymd
$date "+%Y%m%d"
# 10日後の日付を表示
date "+%Y/%m/%d" -d "10 days"   # days,week,month, year 
#root より、時刻の調整
date -s "2022/06/13 12:34:00"
$ date --help
Usage: date [OPTION]... [+FORMAT]
  or:  date [-u|--utc|--universal] [MMDDhhmm[[CC]YY][.ss]]
Display the current time in the given FORMAT, or set the system date.

Mandatory arguments to long options are mandatory for short options too.
  -d, --date=STRING         display time described by STRING, not 'now'
  -f, --file=DATEFILE       like --date once for each line of DATEFILE
  -I[TIMESPEC], --iso-8601[=TIMESPEC]  output date/time in ISO 8601 format.
                            TIMESPEC='date' for date only (the default),
                            'hours', 'minutes', 'seconds', or 'ns' for date
                            and time to the indicated precision.
  -r, --reference=FILE      display the last modification time of FILE
  -R, --rfc-2822            output date and time in RFC 2822 format.
                            Example: Mon, 07 Aug 2006 12:34:56 -0600
      --rfc-3339=TIMESPEC   output date and time in RFC 3339 format.
                            TIMESPEC='date', 'seconds', or 'ns' for
                            date and time to the indicated precision.
                            Date and time components are separated by
                            a single space: 2006-08-07 12:34:56-06:00
  -s, --set=STRING          set time described by STRING
  -u, --utc, --universal    print or set Coordinated Universal Time (UTC)
      --help     display this help and exit
      --version  output version information and exit

FORMAT controls the output.  Interpreted sequences are:

  %%   a literal %
  %a   locale's abbreviated weekday name (e.g., Sun)
  %A   locale's full weekday name (e.g., Sunday)
  %b   locale's abbreviated month name (e.g., Jan)
  %B   locale's full month name (e.g., January)
  %c   locale's date and time (e.g., Thu Mar  3 23:05:25 2005)
  %C   century; like %Y, except omit last two digits (e.g., 20)
  %d   day of month (e.g., 01)
  %D   date; same as %m/%d/%y
  %e   day of month, space padded; same as %_d
  %F   full date; same as %Y-%m-%d
  %g   last two digits of year of ISO week number (see %G)
  %G   year of ISO week number (see %V); normally useful only with %V
  %h   same as %b
  %H   hour (00..23)
  %I   hour (01..12)
  %j   day of year (001..366)
  %k   hour, space padded ( 0..23); same as %_H
  %l   hour, space padded ( 1..12); same as %_I
  %m   month (01..12)
  %M   minute (00..59)
  %n   a newline
  %N   nanoseconds (000000000..999999999)
  %p   locale's equivalent of either AM or PM; blank if not known
  %P   like %p, but lower case
  %r   locale's 12-hour clock time (e.g., 11:11:04 PM)
  %R   24-hour hour and minute; same as %H:%M
  %s   seconds since 1970-01-01 00:00:00 UTC
  %S   second (00..60)
  %t   a tab
  %T   time; same as %H:%M:%S
  %u   day of week (1..7); 1 is Monday
  %U   week number of year, with Sunday as first day of week (00..53)
  %V   ISO week number, with Monday as first day of week (01..53)
  %w   day of week (0..6); 0 is Sunday
  %W   week number of year, with Monday as first day of week (00..53)
  %x   locale's date representation (e.g., 12/31/99)
  %X   locale's time representation (e.g., 23:13:48)
  %y   last two digits of year (00..99)
  %Y   year
  %z   +hhmm numeric time zone (e.g., -0400)
  %:z  +hh:mm numeric time zone (e.g., -04:00)
  %::z  +hh:mm:ss numeric time zone (e.g., -04:00:00)
  %:::z  numeric time zone with : to necessary precision (e.g., -04, +05:30)
  %Z   alphabetic time zone abbreviation (e.g., EDT)

By default, date pads numeric fields with zeroes.
The following optional flags may follow '%':

  -  (hyphen) do not pad the field
  _  (underscore) pad with spaces
  0  (zero) pad with zeros
  ^  use upper case if possible
  #  use opposite case if possible

After any flags comes an optional field width, as a decimal number;
then an optional modifier, which is either
E to use the locale's alternate representations if available, or
O to use the locale's alternate numeric symbols if available.

Examples:
Convert seconds since the epoch (1970-01-01 UTC) to a date
  $ date --date='@2147483647'

Show the time on the west coast of the US (use tzselect(1) to find TZ)
  $ TZ='America/Los_Angeles' date

Show the local time for 9AM next Friday on the west coast of the US
  $ date --date='TZ="America/Los_Angeles" 09:00 next Fri'

GNU coreutils online help: <http://www.gnu.org/software/coreutils/>
For complete documentation, run: info coreutils 'date invocation'

2022年6月7日星期二

gitlab webhook shell

事前準備

1. webhook docker run.sh 

```
docker run -u root --rm -d -p 9000:9000 \

-v /home/$(whoami)/data/webhook:/etc/webhook \
--name=webhook \
almir/webhook -verbose -hooks=/etc/webhook/hooks.json -hotreload -logfile=/etc/webhook/run.tlog

```

2. hooks.json for gitlab merge request state=merged

#=================

[
  {
    "id": "redeploy",
    "execute-command": "/etc/webhook/redeploy.sh",
    "command-working-directory": "/etc/webhook",
    "response-message": "Executing deploy script...\n",
    "pass-arguments-to-command":
    [
      {
        "source": "payload",
        "name": "project.namespace"
      },
      {
        "source": "payload",
        "name": "project.name"
      },
      {
        "source": "payload",
        "name": "event_type"
      },
      {
        "source": "payload",
        "name": "object_attributes.state"
      },
    ],
    "trigger-rule":
    {
      "and":
      [
      {
      "match":
      {
        "type": "value",
        "value": "{you-webhook-token}",
        "parameter":
        {
          "source": "header",
          "name": "X-Gitlab-Token"
        }
      }   
      },
      {
      "match":
      {
        "type": "regex",
        "regex": "merge_request",
        "parameter":
        {
          "source": "payload",
          "name": "event_type"
        }
      }   
      },
      {
      "match":
      {
        "type": "regex",
        "regex": "merged",
        "parameter":
        {
          "source": "payload",
          "name": "object_attributes.state"
        }
      }
      }
      ]
    }
  }
]

#=================


3. redeploy.sh

#!/bin/sh    #required. format error if no this.
#$1=project.namespace (groupname)
#$2=project.name

echo "$1_$2" > /etc/webhook/$1_$2.log

#l read and write log by anyone 
chmod 666 *.log

4. test webhook 

curl http://localhost:9000/hooks/{id}

6. set webhook url to gitlab 

gitlab/group/project/setting

set webhook url and do test. 

7. cicd.sh

logfile="/etc/webhook/groupname/projectname.log"
#check filetimestamp.
lastModifytime=$(date )


while true
do 

  if [ -e $logfile ]; then
    nowtime=$(stat -c $logfile | grep 'Modify: ' | cut -d' ' -f2,3,4)

    if [ $nowtime -gt $lastModifytime ]; then 

        lastModifytime=$nowtime

        #remove first 
        #rm $logfile
        #do git pull and resync
        cd $git_project_folder  
        git pull 
        resync $from_git_path $to_webserver_path

     fi  
  else

    #sleep 5s
    sleep 5  

  fi   

done 

repo path

 repo path=

https://servername/modulename/versionid/modulename-versionid.zip

2022年6月3日星期五

docker webhook

 1.   hooks.yaml 

- id: redeploy-webhook

  execute-command: "/etc/webhook/redeploy.sh"

  command-working-directory: "/etc/webhook"

  

2.  redeploy.sh
#!/bin/bash
date > /tmp/result_test_shell.log
echo $0 > /tmp/result_test_shell.log

3. docker run.sh

docker run -d -p 9000:9000 \
-v /home/ec2-user/data/webhook:/etc/webhook  \
-v /home/ec2-user/data/develop/imartstorage:/var/imartstorage \
-v /home/ec2-user/data/develop/imartstorage:/var/imartstorage \
-v /home/ec2-user/develop: /var/develop \ 
-v /home/ec2-user/develop: /var/develop \ 
-v /home/ec2-user/resindata: /var/resindata\  
 --name=webhook   almir/webhook -verbose -hooks=/etc/webhook/hooks.yaml -hotreload

2. 


2022年5月19日星期四

Linux useradd userdel usermod passwd gpasswd

#create new user names tester 
sudo useradd tester

# add user to group 
sudo usermod -a -G group1,group2 username

# remove user from group sudo gpasswd -d username groupname

#delete user tester
sudo userdel  tester 

sudo userdel  -r tester  # delete files tester created. 

#completed delete user .forbidden
[root@localhost ~]# id tester 
  1000
[root@localhost ~]# find / -uid 1000
/home/tester
/home/tester/.log
/home/tester/.git
[root@localhost ~]# find / -user tester -exec rm -rf {} \;

#set password to user tester 
sodo passwd tester 

#show groups
 cat /etc/group

# create a group sudo groupadd groupname

#delete a group sudo groupdel groupname

#change a user's primary group sudo usermod -g groupname username sudo usermod -g developers tester

# append group to user
sudo usermod -aG groupname username # remove group from user 
 gpasswd -d username 
groupname 

#show users in group
sudo groups groupname





2022年5月18日星期三

linux zip unzip

$ zip sample file1 file2 
adding: file1 (stored 0%) adding: file2 (stored 0%) $ ls sample.zip file1 file2
unzip sample.zip -d /var/www/html


nzip

UnZip 6.00 of 20 April 2009, by Debian. Original by Info-ZIP.


Usage: unzip [-Z] [-opts[modifiers]] file[.zip] [list] [-x xlist] [-d exdir]

  Default action is to extract files in list, except those in xlist, to exdir;

  file[.zip] may be a wildcard.  -Z => ZipInfo mode ("unzip -Z" for usage).


  -p  extract files to pipe, no messages     -l  list files (short format)

  -f  freshen existing files, create none    -t  test compressed archive data

  -u  update files, create if necessary      -z  display archive comment only

  -v  list verbosely/show version info       -T  timestamp archive to latest

  -x  exclude files that follow (in xlist)   -d  extract files into exdir

modifiers:

  -n  never overwrite existing files         -q  quiet mode (-qq => quieter)

  -o  overwrite files WITHOUT prompting      -a  auto-convert any text files

  -j  junk paths (do not make directories)   -aa treat ALL files as text

  -U  use escapes for all non-ASCII Unicode  -UU ignore any Unicode fields

  -C  match filenames case-insensitively     -L  make (some) names lowercase

  -X  restore UID/GID info                   -V  retain VMS version numbers

  -K  keep setuid/setgid/tacky permissions   -M  pipe through "more" pager

  -O CHARSET  specify a character encoding for DOS, Windows and OS/2 archives

  -I CHARSET  specify a character encoding for UNIX and other archives


See "unzip -hh" or unzip.txt for more help.  Examples:

  unzip data1 -x joe   => extract all files except joe from zipfile data1.zip

  unzip -p foo | more  => send contents of foo.zip via pipe into program more

  unzip -fo foo ReadMe => quietly replace existing ReadMe if archive file newer