[长期更新] Linux简单实用的功能

1. 一觉醒来阿里云的机器语言变成中文了,怎么办?

export LANG="en_US.UTF-8"

export LANGUAGE="en_US:en"


2. 阿里云的Ubuntu默认使用dash,不支持source等,如何把默认的shell换成更加通用的bash?

sudo dpkg-reconfigure dash

在弹出窗口选项中选择 NO即可


3. crontab的默认编辑器被换成了Nano,怎么换回Vim?

执行命令:select-editor

选择Vim即可


4. 远程执行命令

ssh -p port user@host 'cmd'


5. 查看端口使用情况

 lsof -i tcp:port 


6. git执行pull或者push需要输入密码,把ssh-key加进去也没用


看看你的remote origin,如果是https那么每次进行同步都是需要密码的,需要使用ssh url,把.git/config中的url 改成git@bitbucket.org/XXXXXX就可以了


7. 有个文本文件,我想在每行前加一个行号用于后续的处理


cat filename | grep -n '^' | sed 's/\:/\t/g' > new_filename ,即先用grep为每行添加行号,输出格式为row:xxx xxx,然后使用sed把冒号替换成tab


8. maven想使用本地的jar包


参考 http://stackoverflow.com/questions/28623900/how-do-i-add-a-jar-file-to-my-local-maven-repository-using-eclipse-m2e-on-luna-s


9. 使用R+ggplot在绘制中国地图的热力图时,读取bou2_4p.shp文件后,拿到的省市信息是乱码无法正常显示


as.character(na.omit(unique(iconv(x@data$NAME,"GBK","UTF-8"))));

参考http://blog.csdn.net/lichangzai/article/details/39478871


10. 在Vim中如何输入Unicode字符,例如\u2028

首先需要必须设置编码为utf-8:

set encoding=utf-8
  • 1
  • 1

在insert或command模式(不能在normal模式)下,按Ctrl+v 
然后再按u, 接着按该unicode字符所对应的16进制编号2028即可

11.  java占用的虚拟内存(htop下的virt)过高,怎么回事,怎么办?

参考http://www.cnblogs.com/seasonsluo/p/java_virt.html

现代操作系统里面分配虚拟地址空间操作不同于分配物理内存。在64位操作系统上,可用的最大虚拟地址空间有16EB,即大概180亿GB。那么在一台只有16G的物理内存的机器上,我也能要求获得4TB的地址空间以备将来使用。例如:

    void *mem = mmap(0, 4ul * 1024ul * 1024ul * 1024ul * 1024ul,
                     PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS | MAP_NORESERVE,
                     -1, 0);
当使用 mmap 并设置 MAP_NORESERVE 标志时,并不会要求实际的物理内存和swap空间存在。所以上述代码可以在top中看到使用了 4096g 的 VIRT 虚拟内存,这当然是不可能的,它只是表示使用了 4096GB 的地址空间而已。

glibc分配内存的时候,大内存从从中央分配区分配,小内存从线程创建时,预先分配的缓存区分配。glibc为了分配内存的性能的问题,使用了很多叫做arena的memory pool,缺省配置在64bit下面是每一个arena为64M,一个进程可以最多有 cores * 8个arena。假设你的机器是4核的,那么最多可以有4 * 8 = 32个arena,也就是使用32 * 64 = 2048M内存。

所以

  • VIRT高是因为分配了太多地址空间导致。
  • 一般来说不用太在意VIRT太高,因为你有16EB的空间可以使用。


12. tomcat7 启动太慢,日志提示:


May 05, 2017 12:13:26 PM org.apache.catalina.util.SessionIdGeneratorBase createSecureRandom
INFO: Creation of SecureRandom instance for session ID generation using [SHA1PRNG] took [304,316] milliseconds.

经排查,是entropy source导致的问题,解决方案:haveged

https://www.digitalocean.com/community/tutorials/how-to-setup-additional-entropy-for-cloud-servers-using-haveged

然后sudo service haveged start即可  


13. maven工程在执行maven install的时候,提示 use -source 7 or higher to enable try-with-resources


在pom.xml中修改 maven-compiler-plugin 的版本

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.1</version>
    <configuration>
        <source>1.7</source>
        <target>1.7</target>
    </configuration>
</plugin>

http://stackoverflow.com/questions/29258141/maven-compilation-error-use-source-7-or-higher-to-enable-diamond-operator


14. maven在打包时,移除自己不想要的文件


如果是项目依赖,一般放在src/main/resources目录下,而如果是测试依赖文件,一般放在src/test/resources目录下

如果是某些类不想打包,就在maven-compiler-plugin插件下写exclude模块,而如果是依赖文件不想要,就在pom的build模块中写resources模块,指明目录,然后指明哪些要include哪些要exclude

如:

<build>
		<resources>
			<resource>
				<directory>src/test/resources</directory>
				<excludes>
					<exclude>**/*</exclude>
				</excludes>
			</resource>
		</resources>
		<plugins>
			<plugin>
				<artifactId>maven-compiler-plugin</artifactId>
				<version>3.0</version>
				<configuration>
					<source>1.7</source>
					<target>1.7</target>
				</configuration>
			</plugin>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-jar-plugin</artifactId>
				<version>2.3.1</version>
			</plugin>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-surefire-plugin</artifactId>
				<version>2.12</version>
				<dependencies>
					<dependency>
						<groupId>org.apache.maven.surefire</groupId>
						<artifactId>surefire-junit47</artifactId>
						<version>2.12</version>
					</dependency>
				</dependencies>
			</plugin>
		</plugins>
	</build>

15. 查看git项目的代码行数

git ls-files | xargs wc-l

如果是看所有java文件的代码行数:

git ls-files *.java |xargs wc -l


16. 使用service启动server上的rsync时,提示"rsync daemon not enabled in /etc/default/rsync, not starting..." 

因为 /etc/default/rsync文件中的RSYNC_ENABLE=false所以导致rsync无法启用

把RSYNC_ENABLE设置为true即可成功启动rsync用于文件同步

https://ubuntuforums.org/showthread.php?t=1252720


17. 使用SHELL获取UTC时间  

date -u "+%Y-%m-%dT%H:%M:%SZ"


18. CentOS系统下,tomcat的日志全部replicate到/var/log/messages中

解决方案:tomcat prints logging output to /var/log/messages


19. 使用COPY命令把文件中的内容同步到postgresql,报错

copy和\copy有非常大的差异。

copy是DB 的server用的命令,如果用psql的client,那应该用\copy。

考虑到之前的server上,自建的DB只有一个user,所以用copy也可以,因为执行者既是server,又是client

当用户多起来以后,我们使用特定用户作为client执行copy就必须用\copy了。

参见:Postgres ERROR: could not open file for reading: Permission denied


20. vim对数据排序

1,$!sort -k 4 -t"." -n 

这是对全文档进行排序,-k对应的是第几列,-t对应的是用什么来分割,-n是把该列元素用数字来比较进而排序,否则默认用字符串对比  


21. crontab的编辑器是vim,但是没有高亮  

因为默认EDITOR是空,即会使用vi当默认编辑器。要显示的替换成vim才会有对应的高亮

export EDITOR=vim


22. CentOS在使用git时,git status文件没有高亮显示  

编辑.gitconfig文件,加入如下内容即可

[color]
  diff = auto
  status = auto
  branch = auto
  interactive = auto
  ui = true
  pager = true


23. CentOS 守护进程

最近centos的tomcat因为内存问题crash了,但是没有自动restart,需要配置守护进程

参考

systemd - CentOS 7进程守护&监控点击打开链接

How To Configure a Linux Service to Start Automatically After a Crash or Reboot – Part 1: Practical Examples


24. postgresql用python写udf

查看postgresql的语言是否支持python select * from pg_language

如果没有plpythonu,则需要安装posgtresql的python支持包 

sudo apt-get install postgresql-contrib-9.4 postgresql-plpython-9.4

然后create language plpythonu

接下来就可以按照如下格式写udf了

CREATE or replace function extract_value_by_key(s text, k text)
	RETURNS text
AS \$extract_value_by_key\$
	arr = s.split(',')
	for ele in arr:
		ele = ele.strip()
		if k in ele:
			return ele[len(k)+2:].lower()
	return None		
\$extract_value_by_key\$ LANGUAGE plpythonu;






  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值