linux上使用flock来调用程序

在使用linux的crontab定时调用程序时会经常判断程序是否已经启用,之前是直接在程序中判断程序是否启动。后来找到一种比较简洁的方法flock,它是一种文件锁的方式。

[adadmin@s11 ~]$ flock
flock (util-linux-ng 2.17.2)
Usage: flock [-sxun][-w #] fd#
flock [-sxon][-w #] file [-c] command…
flock [-sxon][-w #] directory [-c] command…
-s –shared Get a shared lock
-x –exclusive Get an exclusive lock
-u –unlock Remove a lock
-n –nonblock Fail rather than wait
-w –timeout Wait for a limited amount of time
-o –close Close file descriptor before running command
-c –command Run a single command string through the shell
-h –help Display this text
-V –version Display version

 

使用如下:

先创建一个文件

touch test_flock.lock

调用如下(以python test_flock.py为示例)

flock -nx test_flock.lock python test_flock.py

 

python打包成exe执行文件

环境:windows 7,  python 2.7

写python文件格式.py的程序需要将其打包成可执行的文件形式,可以使用PyInstaller来打包。

下载PyInstaller-3.1文件,使用打包命令如下:

D:\\program\\PyInstaller-3.1>pyinstaller.py -F ../../qs123/s3test.py –upx-dir upx391w

此命令将其打包成一个可执行文件并进行压缩。

 

参数:

-F 指定打包后只生成一个exe格式的文件

-D –onedir 创建一个目录,包含exe文件,但会依赖很多文件(默认选项)

-c –console, –nowindowed 使用控制台,无界面(默认)

-w –windowed, –noconsole 使用窗口,无控制台

-p 添加搜索路径,让其找到对应的库。

-i 改变生成程序的icon图标

 

 

Linux history的详解

经常使用linux内置的history命令查看历史,在当前用户下面有个.bash_history文件用于保存历史命令,另外$HISTSIZE环境变量是保存最大条数。在当前shell环境中,命令放在内存中,退出时将最近$HISTSIZE条命令保存在.bash_history上,也可以使用history -a(从登录起到现在的命令)手工保存,另外history -w将当前命令保存下来。如果想命令立刻保存下来,可以在.bashrc中设置环境变量export PROMPT_COMMAND=’history -a’

tmp目录下隐藏文件

环境: CentOS 6.2

由于/tmp目录下只有一个g的容量,但发现有600M的空间被占用。在tmp目录下du -sm却只有不到10M的文件。最后发现是运行的应用程序写日志文件被删除了,

通过命令查看  lsof /tmp | grep deleted

解决方法,关闭相应的应用程序并记住不要轻易删除tmp目录下的文件。

librados: client.admin initialization error (2) No such file or directory

环境: Ubuntu 14.04, Ceph 0.80.10

在使用ceph或rados命令时(如:rados -p .rgw ls)发生只能加sudo才可以使用,不然就出现报错:

monclient(hunting): ERROR: missing keyring, cannot use cephx for authentication

librados: client.admin initialization error (2) No such file or directory

 

ls /etc/ceph/ceph.client.admin.keyring -l
-rw——- 1 root root 63 Jan 18 18:45 /etc/ceph/ceph.client.admin.keyring

查看文件的属性才发现只有root用户才可以读取文件ceph.client.admin.keyring。因而修改其权限以便其它用户也可以访问

sudo chmod o+r /etc/ceph/ceph.client.admin.keyring

apt-get update 问题

环境:Ubuntu 14.04

使用apt-get update出现如下问题:

Fetched 11.8 MB in 26s (448 kB/s)
W: GPG error: http://security.debian.org wheezy/updates InRelease: The following signatures couldn’t be verified because the public key is not available: NO_PUBKEY 9D6D8F6BC857C906 NO_PUBKEY 8B48AD6246925553
W: GPG error: http://http.debian.net wheezy-updates Release: The following signatures couldn’t be verified because the public key is not available: NO_PUBKEY 8B48AD6246925553 NO_PUBKEY 7638D0442B90D010
W: Failed to fetch http://packages.couchbase.com/ubuntu/dists/trusty/InRelease Unable to find expected entry ‘precise/main/binary-amd64/Packages’ in Release file (Wrong sources.list entry or malformed file)

W: Failed to fetch http://http.debian.net/debian/dists/wheezy/Release.gpg Connection failed

 

解决方法,增加公共的key:
sudo apt-key adv –keyserver keyserver.ubuntu.com –recv-keys 9D6D8F6BC857C906 8B48AD6246925553 7638D0442B90D010 6FB2A1C265FFB764