分类目录归档:Linux

centos下supervisor安装和使用

安装

dnf update -y
dnf install supervisor
sytemctl enable supervisord

配置

vim /etc/supervisor/xxx.conf

    [program:testprogram]
    directory = /root/go/src/test
    command = /root/go/src/test/bin/server
    autostart = true
    startsecs = 5
    autorestart = true
    startretries = 3
    user = root
    redirect_stderr = true
    stdout_logfile = /var/log/supervisor/test.log

使用

supervisorctl reload
supervisorctl start xxx
supervisorctl status

Linux Shell生成随机数和随机字符串的5种方法

使用系统的 $RANDOM 变量

(CentOS、Ubuntu、MacOS 都支持,但只有5位数随机)

echo $RANDOM
24308

使用date +%s%N

(CentOS、Ubuntu支持,MacOS不支持纳秒 +%N)

date +%s%N
1557366636971317933

使用 /dev/random 和 /dev/urandom 随机文件

(CentOS、Ubuntu、MacOS 都支持)

head -20 /dev/urandom | cksum | cut -f1 -d " "
1053195602

使用 linux uuid

(CentOS、Ubuntu支持,MacOS不支持)

cat /proc/sys/kernel/random/uuid
bf05d41c-d69e-4247-8a79-baf3ab3f98ed

使用 openssl rand

(CentOS、Ubuntu支持、MacOS 都支持,需安装 openssl,推荐)

openssl rand -base64 128 
Smt+jcaMSQ64Y4La8U6OUFl/TZWm+cKGtDqg+MRbKY6Zwp5iOzg3xoVZv+6NQv/W 
UL+sqAHBhbTD1ZPgsyeloxTVLRV/SjU2O/mJAZoFLLZXtDhldl5lfgz3s3s28R+u 
24TUNXnxGLGqQQxaudcF8Lhgq3LjDLQWzrgrTOIv1e0=

openssl rand -base64 128 | md5sum | cut -c1-32 
2cbb48ea9bac4b08c2a9b4518e1faf79

openssl rand -hex 16 
339a905406bb4ef3631dfa1ad67d8d6a

openssl rand -hex 8 | cksum 
307618125 17

openssl rand -hex 8 | cksum | cut -c1-8 
41657201

fedora如何从Nautilus/Files打开终端

使用Files(又名Nautilus)时,您是否想过快速打开终端以在当前位置运行命令?

Nautilus的gnome-terminal-nautilus附加组件提供了一个右键单击上下文菜单项, 可以在当前位置快速打开一个新的gnome-terminal窗口。

enter image description here

当您需要对图形文件浏览器中正在查看的某些文件运行快速命令时,对nautilus界面的这一小调整非常方便。

安装gnome-terminal-nautilus

sudo dnf install gnome-terminal-nautilus

安装后,“在终端中打开”选项应出现在“文件”中的右键单击上下文菜单中。如果安装后没有出现,请通过运行以下命令强制关闭Nautilus。然后重新打开文件

nautilus -q

解决nginx+php+yii2下time_wait连接数过多问题

查看连接数

netstat -n |  wc -l
netstat -n | grep -i time_wait | wc -l

发现9000和3306的time_out状态的连接数过多, 网上大部分解决方案是修改sysctl.conf回收重用ipv4连接, 但是这在公司的阿里云机器上好像行不通,回导致无法访问。

tcp复用解决方案

net.ipv4.tcp_syncookies = 1
net.ipv4.tcp_tw_reuse=1
net.ipv4.tcp_tw_recycle=1
net.ipv4.tcp_fin_timeout=30

解决nginx time_wait连接数过多问题

修改nginx.conf

upstream fastcgi_backend {
    server 127.0.0.1:9000;
    keepalive 60;
}

location ~ \.php$ {
        fastcgi_pass  fastcgi_backend;
        fastcgi_keep_conn on;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include        fastcgi_params;
}

解决mysql time_out连接数过多

修改yii数据库配置采用长链接

'db' => [ 
    'class' => 'yii\db\Connection',
    'dsn' => '*', 'username' => '*', 
    'password' => '*', 
    'charset' => 'utf8mb4', 
    'attributes' => [ 
       PDO::ATTR_PERSISTENT => true 
    ]
],

linux下PhpStorm,WebStorm,PyChram,IntelliJ IDEA,Android Studio使用ibus,fcitx输入中文

找到在pycharm安装目录bin下的pycharm.sh文件

在pycharm.sh文件中添加以下代码:

export GTK_IM_MODULE=ibus 
export QT_IM_MODULE=ibus 
export XMODIFIERS=@im=ibus

添加的位置

在Run the IDE之前,添加到文件末尾无效

CLASSPATH="$IDE_HOME/lib/bootstrap.jar"
CLASSPATH="$CLASSPATH:$IDE_HOME/lib/extensions.jar"
CLASSPATH="$CLASSPATH:$IDE_HOME/lib/util.jar"
CLASSPATH="$CLASSPATH:$IDE_HOME/lib/jdom.jar"
CLASSPATH="$CLASSPATH:$IDE_HOME/lib/log4j.jar"
CLASSPATH="$CLASSPATH:$IDE_HOME/lib/trove4j.jar"
CLASSPATH="$CLASSPATH:$IDE_HOME/lib/jna.jar"
if [ -n "$PYCHARM_CLASSPATH" ]; then
  CLASSPATH="$CLASSPATH:$PYCHARM_CLASSPATH"
fi

export GTK_IM_MODULE=ibus 
export QT_IM_MODULE=ibus 
export XMODIFIERS=@im=ibus

# ---------------------------------------------------------------------
# Run the IDE.
# ---------------------------------------------------------------------

如果使用fcitx输入法将ibus修改为fcitx即可

CentOS7通过yum安装PostgreSQL10

发现最新的postgresql版本

https://www.postgresql.org/download/linux/redhat/

下载

yum install https://download.postgresql.org/pub/repos/yum/10/redhat/rhel-7-x86_64/pgdg-centos10-10-2.noarch.rpm

安装

yum install postgresql10-server

设置数据目录

创建数据目录并修改权限

mkdir -p /data/postgresql/
chown postgres:postgres /data/postgresql/

复制开机启动文件

sudo cp /usr/lib/systemd/system/postgresql-10.service /etc/systemd/system/

修改开机启动文件

vim /etc/systemd/system/postgresql-10.service

找到

Environment=PGDATA=/var/lib/pgsql/10/data/

修改为

Environment=PGDATA=/data/postgresql/

初始化数据库

/usr/pgsql-10/bin/postgresql-10-setup initdb

开机自启动

systemctl enable postgresql-10
systemctl start postgresql-10
systemctl status postgresql-10

修改本地帐户权限

vim /data/postgresql/pg_hba.conf

host    all             all             127.0.0.1/32            ident

修改为

host    all             all             127.0.0.1/32            md5

重新加载配置

systemctl restart postgresql-10

新增用户和数据库

su - postgres
createuser dbuser
createdb -e -O dbuser dbname

设定密码

su - postgres
psql
\password dbuser (输入两次密码)

新用户登录数据库

psql -U dbuser -d dbname -h 127.0.0.1 (输入之前的密码)

允许非本机ip登录

  • 编辑data/postgresql.conf修改listen_addresses = 'localhost'listen_addresses = 'localhost'
  • 编辑data/pg_hba.conf

    host all all 192.168.1.0/24 md5

fedora27+python3+virtualenv+virtualenvwrapper安装及使用

安装virtualenvwrapper

这里使用pip3进行安装即可

sudo pip3 install virtualenvwrapper

设置virtualenvwrapper的运行环境变量

编辑~/.bashrc,加入以下几行

VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3 
export WORKON_HOME='~/.virtualenvs'
source /usr/local/bin/virtualenvwrapper.sh

使环境变量马上生效:

source ~/.bashrc

virtualenvwrapper使用

mkvirtualenv 新建虚拟环境

mkvirtualenv env1

建立后可以看到终端会以env1开头

(env1) [ning@localhost]$

再建立一个env2

mkvirtualenv env2

workon 启动/切换虚拟环境

workon env1

deactivate 离开虚拟环境

deactivate

rmvirtualenv 删除虚拟环境

rmvirtualenv env2

virtualenvwrapper help 查看virtualenvwrapper使用帮助

virtualenvwrapper is a set of extensions to Ian Bicking's virtualenv
tool.  The extensions include wrappers for creating and deleting
virtual environments and otherwise managing your development workflow,
making it easier to work on more than one project at a time without
introducing conflicts in their dependencies.

For more information please refer to the documentation:

    http://virtualenvwrapper.readthedocs.org/en/latest/command_ref.html

Commands available:

  add2virtualenv: add directory to the import path

  allvirtualenv: run a command in all virtualenvs

  cdproject: change directory to the active project

  cdsitepackages: change to the site-packages directory

  cdvirtualenv: change to the $VIRTUAL_ENV directory

  cpvirtualenv: duplicate the named virtualenv to make a new one

  lssitepackages: list contents of the site-packages directory

  lsvirtualenv: list virtualenvs

  mkproject: create a new project directory and its associated virtualenv

  mktmpenv: create a temporary virtualenv

  mkvirtualenv: Create a new virtualenv in $WORKON_HOME

  rmvirtualenv: Remove a virtualenv

  setvirtualenvproject: associate a project directory with a virtualenv

  showvirtualenv: show details of a single virtualenv

  toggleglobalsitepackages: turn access to global site-packages on/off

  virtualenvwrapper: show this help message

  wipeenv: remove all packages installed in the current virtualenv

  workon: list or change working virtualenvs

virtualenv --help 帮助

Usage: virtualenv [OPTIONS] DEST_DIR

Options:
  --version             show program's version number and exit
  -h, --help            show this help message and exit
  -v, --verbose         Increase verbosity.
  -q, --quiet           Decrease verbosity.
  -p PYTHON_EXE, --python=PYTHON_EXE
                        The Python interpreter to use, e.g.,
                        --python=python2.5 will use the python2.5 interpreter
                        to create the new environment.  The default is the
                        interpreter that virtualenv was installed with
                        (/usr/bin/python3)
  --clear               Clear out the non-root install and start from scratch.
  --no-site-packages    DEPRECATED. Retained only for backward compatibility.
                        Not having access to global site-packages is now the
                        default behavior.
  --system-site-packages
                        Give the virtual environment access to the global
                        site-packages.
  --always-copy         Always copy files rather than symlinking.
  --unzip-setuptools    Unzip Setuptools when installing it.
  --relocatable         Make an EXISTING virtualenv environment relocatable.
                        This fixes up scripts and makes all .pth files
                        relative.
  --no-setuptools       Do not install setuptools in the new virtualenv.
  --no-pip              Do not install pip in the new virtualenv.
  --no-wheel            Do not install wheel in the new virtualenv.
  --extra-search-dir=DIR
                        Directory to look for setuptools/pip distributions in.
                        This option can be used multiple times.
  --download            Download preinstalled packages from PyPI.
  --no-download, --never-download
                        Do not download preinstalled packages from PyPI.
  --prompt=PROMPT       Provides an alternative prompt prefix for this
                        environment.
  --setuptools          DEPRECATED. Retained only for backward compatibility.
                        This option has no effect.
  --distribute          DEPRECATED. Retained only for backward compatibility.
                        This option has no effect.