分类 【操作系统】 下的文章

windows上自动设置java环境变量的脚本

@echo off

:: TODO:设置java环境变量
:: Author: Gwt
color 02
::设置java的安装路径,可方便切换不同的版本
set input=
set /p "input=请输入java的jdk路径(或回车默认路径为C:\Program Files\Java\jdk1.7.0_71):"
if defined input (echo jdk已设置) else (set input=C:\Program Files\Java\jdk1.7.0_71)
echo jdk路径为%input%
set javaPath=%input%

::如果有的话,先删除JAVA_HOME
wmic ENVIRONMENT where "name='JAVA_HOME'" delete

::如果有的话,先删除ClASS_PATH 
wmic ENVIRONMENT where "name='CLASS_PATH'" delete

::创建JAVA_HOME
wmic ENVIRONMENT create name="JAVA_HOME",username="<system>",VariableValue="%javaPath%"

::创建CLASS_PATH
wmic ENVIRONMENT create name="CLASS_PATH",username="<system>",VariableValue=".;%%JAVA_HOME%%\lib\tools.jar;%

%JAVA_HOME%%\lib\dt.jar;"

::在环境变量path中,剔除掉变量java_home中的字符,回显剩下的字符串
call set xx=%Path%;%JAVA_HOME%\jre\bin;%JAVA_HOME%\bin

::echo %xx%

::将返回显的字符重新赋值到path中
wmic ENVIRONMENT where "name='Path' and username='<system>'" set VariableValue="%xx%"

pause

解释下命令

   @echo off 是关闭回显的,不会显示命令信息 on打开会显示命令信息

  color 02是设置输出文本颜色的,这里是控制命令台输出绿颜色

  set /p "input=请输入命令信息" 是用来接收控制台输入的文本信息的

  if else 是用来做判断 if defined input 是用来判断用户是否输入信息,回车的话,则表示未定义input的值

  echo "输出信息" 是用来显示信息的

  set javaPath=%input% 是用来吧变量input的值赋值给javaPath变量的

  wmic 是提供了批处理的命令,可以方便的操作环境变量值

  删除某一环境变量 就使用 wmic ENVIRONMENT where "name='JAVA_HOME'" delete命令

  创建某一环境变量 使用 wmic ENVIRONMENT create name="JAVA_HOME",username="<system>",VariableValue="%javaPath%"命令

      修改某一环境变量 使用 wmic ENVIRONMENT where "name='Path' and username='<system>'" set VariableValue="value"

注意事项:

  1、在上述批处理脚本命令中,要严格注意空格和大小写等的输入,不然执行不成功的

  2、要以管理员方式运行命令,不然提示拒绝执行命令的提示

解决MacOS下readlink: illegal option -- f

Mac下的readlink没有-f参数,诸如screenfetch又会去调用readlink -f,于是每次都会出现:

readlink: illegal option -- f
usage: readlink [-n] [file ...]

很是烦人,解决方案如下:
1.安装coreutils:

brew install coreutils

2.设置环境变量,编辑~/.bash_profile,添加:

export PATH="/usr/local/opt/coreutils/libexec/gnubin:$PATH"

3.创建别名,使用greadlink替代readlink,同样是编辑~/.bash_profile,添加:

alias readlink=greadlink

centos 6.x 配置iptables

#!/bin/bash
#清除现有所有规则
iptables -F
iptables -X
iptables -Z

#允许icmp包进入
iptables -A INPUT -p icmp --icmp-type any -j ACCEPT
#允许本地的数据包
iptables -A INPUT -s localhost -d localhost -j ACCEPT
#允许已经建立和相关的数据包进入
iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
#允许icmp包出去
iptables -A OUTPUT -p icmp --icmp any -j ACCEPT
 #允许本地数据包
iptables -A OUTPUT -s localhost -d localhost -j ACCEPT
#允许已经建立和相关的数据包出去
iptables -A OUTPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
#允许ESTABLISHED和RELATED状态的包
iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT

#允许有限的ping功能,每秒10次
iptables -A INPUT -p icmp -m limit --limit 10/sec -j ACCEPT

#允许所有本机向外的访问
iptables -A OUTPUT -j ACCEPT

# 允许访问22端口,ssh使用
iptables -A INPUT -p tcp --dport 22 -j ACCEPT

# 允许访问8888端口,ss使用
iptables -A INPUT -p tcp --dport 8888 -j ACCEPT

#允许访问80,443端口
iptables -A INPUT -p tcp --dport 80 -j ACCEPT
iptables -A INPUT -p tcp --dport 443 -j ACCEPT

#禁止其他未允许的规则访问
iptables -A INPUT -j REJECT
iptables -A FORWARD -j REJECT

service iptables save

如果保持失败,是因为缺少policycoreutils,请先安装

yum -y install policycoreutils

将iptables加入随机启动

chkconfig --level 345 iptables on

查看已有规则

iptables -L -n --line-numbe

解决insserv: warning: script '服务名' missing LSB tags and overrides的问题

编写了一个tomcat开机自启动脚本,执行没问题,但是在安装mysql的时候出现了大量的:

insserv: warning: script 'S20tomcat' missing LSB tags and overrides
insserv: warning: script 'tomcat' missing LSB tags and overrides
insserv: There is a loop at service rmnologin if started
insserv: There is a loop between service rmnologin and mountnfs if started
insserv:  loop involving service mountnfs at depth 6
insserv:  loop involving service networking at depth 5
insserv: There is a loop between service tomcat and hwclock if started
insserv:  loop involving service hwclock at depth 3
insserv:  loop involving service mountdevsubfs at depth 2
insserv: There is a loop between service tomcat and udev if started
insserv:  loop involving service udev at depth 1
insserv: Starting tomcat depends on rmnologin and therefore on system facility `$all' which can not be true!
insserv: Starting tomcat depends on rmnologin and therefore on system facility `$all' which can not be true!
insserv: Starting tomcat depends on rmnologin and therefore on system facility `$all' which can not be true!
insserv: Starting tomcat depends on rmnologin and therefore on system facility `$all' which can not be true!
insserv: Starting tomcat depends on rmnologin and therefore on system facility `$all' which can not be true!
insserv: Starting tomcat depends on rmnologin and therefore on system facility `$all' which can not be true!
insserv: Starting tomcat depends on rmnologin and therefore on system facility `$all' which can not be true!
insserv: Starting tomcat depends on rmnologin and therefore on system facility `$all' which can not be true!
insserv: Starting tomcat depends on rmnologin and therefore on system facility `$all' which can not be true!
insserv: Starting tomcat depends on rmnologin and therefore on system facility `$all' which can not be true!
insserv: Starting tomcat depends on rmnologin and therefore on system facility `$all' which can not be true!

居然还导致mysql安装后的自动配置提示失败,按照LSB tags规范改写脚本如下(debian8环境):

#!/bin/bash
### BEGIN INIT INFO
# Provides:          bbzhh.com
# Required-Start:    $local_fs $network
# Required-Stop:     $local_fs
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: tomcat service
# Description:       tomcat service daemon
### END INIT INFO
#
# description: Apache Tomcat init script
# processname: tomcat
# chkconfig: 234 20 80
#
#
# Copyright (C) 2014 Miglen Evlogiev
#
# This program is free software: you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free Software
# Foundation, either version 3 of the License, or (at your option) any later
# version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of  MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along with
# this program.  If not, see <http://www.gnu.org/licenses/>.
#
# Initially forked from: gist.github.com/valotas/1000094
# Source: gist.github.com/miglen/5590986


#Location of JAVA_HOME (bin files)
export JAVA_HOME=/opt/jdk

#Add Java binary files to PATH
export PATH=$JAVA_HOME/bin:$PATH

#CATALINA_HOME is the location of the bin files of Tomcat
export CATALINA_HOME=/opt/tomcat

#CATALINA_BASE is the location of the configuration files of this instance of Tomcat
export CATALINA_BASE=/opt/tomcat
export CATALINA_BOOTSTRAP=/opt/tomcat/bin/bootstrap.jar

#TOMCAT_USER is the default user of tomcat
export TOMCAT_USER=tomcat

#TOMCAT_USAGE is the message if this script is called without any options
TOMCAT_USAGE="Usage: $0 {\e[00;32mstart\e[00m|\e[00;31mstop\e[00m|\e[00;31mkill\e[00m|\e[00;32mstatus\e[00m|\e[00;31mrestart\e[00m}"

#SHUTDOWN_WAIT is wait time in seconds for java proccess to stop
SHUTDOWN_WAIT=20

tomcat_pid() {
        echo `ps -fe | grep $CATALINA_BOOTSTRAP | grep -v grep | tr -s " "|cut -d" " -f2`
}

start() {
  pid=$(tomcat_pid)
  if [ -n "$pid" ]
  then
    echo -e "\e[00;31mTomcat is already running (pid: $pid)\e[00m"
  else
    # Start tomcat
    echo -e "\e[00;32mStarting tomcat\e[00m"
    #ulimit -n 100000
    #umask 007
    #/bin/su -p -s /bin/sh $TOMCAT_USER
        if [ `user_exists $TOMCAT_USER` = "1" ]
        then
                /bin/su $TOMCAT_USER -c $CATALINA_HOME/bin/startup.sh
        else
                echo -e "\e[00;31mTomcat user $TOMCAT_USER does not exists. Starting with $(id)\e[00m"
                sh $CATALINA_HOME/bin/startup.sh
        fi
        status
  fi
  return 0
}

status(){
          pid=$(tomcat_pid)
          if [ -n "$pid" ]
            then echo -e "\e[00;32mTomcat is running with pid: $pid\e[00m"
          else
            echo -e "\e[00;31mTomcat is not running\e[00m"
            return 3
          fi
}

terminate() {
        echo -e "\e[00;31mTerminating Tomcat\e[00m"
        kill -9 $(tomcat_pid)
}

stop() {
  pid=$(tomcat_pid)
  if [ -n "$pid" ]
  then
    echo -e "\e[00;31mStoping Tomcat\e[00m"
    #/bin/su -p -s /bin/sh $TOMCAT_USER
        sh $CATALINA_HOME/bin/shutdown.sh

    let kwait=$SHUTDOWN_WAIT
    count=0;
    until [ `ps -p $pid | grep -c $pid` = '0' ] || [ $count -gt $kwait ]
    do
      echo -n -e "\n\e[00;31mwaiting for processes to exit\e[00m";
      sleep 1
      let count=$count+1;
    done

    if [ $count -gt $kwait ]; then
      echo -n -e "\n\e[00;31mkilling processes didn't stop after $SHUTDOWN_WAIT seconds\e[00m"
      terminate
    fi
  else
    echo -e "\e[00;31mTomcat is not running\e[00m"
  fi

  return 0
}

user_exists(){
        if id -u $1 >/dev/null 2>&1; then
        echo "1"
        else
                echo "0"
        fi
}

case $1 in
        start)
          start
        ;;
        stop)
          stop
        ;;
        restart)
          stop
          start
        ;;
        status)
                status
                exit $?
        ;;
        kill)
                terminate
        ;;
        *)
                echo -e $TOMCAT_USAGE
        ;;
esac
exit 0
                                                                                                      

其实解决办法就是在#!/bin/bash下面添加:

### BEGIN INIT INFO
# Provides:          bbzhh.com
# Required-Start:    $local_fs $network
# Required-Stop:     $local_fs
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: tomcat service
# Description:       tomcat service daemon
### END INIT INFO

当然,也可以一劳永逸的:

apt-get remove insserv

解决 golang在macos编译时fatal error: MSpanList_Insert错误

mac自从抹盘安装macOS Sierra (version 10.12)之后,就一直没有安装golang环境,这几天想写一个运维小工具,思来想去还是golang最合适,编译后的体积虽然大了一些,但是胜在完全不依赖任何环境、任何第三方库,于是开始了golang环境的折腾。
按照以往惯例,安装golang 1.5+以上版本的时候,如果需要解决交叉编译环境的问题时,需要安装golang1.4.x,于是轻车熟路的先brew安装了gvm,然后gvm install go1.4.3,再然后设置$GOROOT_BOOTSTRAP环境变量为go1.4.3的安装路径,再来gvm install go1.7.3,报错如下:

failed MSpanList_Insert 0x905000 0x9b51f3c97b8 0x0 0x0
fatal error: MSpanList_Insert
runtime stack:
runtime.throw(0x5e5cf0, 0x10)
    /home/travis/.gimme/versions/go1.6.linux.amd64/src/runtime/panic.go:530 +0x90 fp=0x7fff5fbff0b0 sp=0x7fff5fbff098
runtime.(*mSpanList).insert(0x865d08, 0x905000)
    /home/travis/.gimme/versions/go1.6.linux.amd64/src/runtime/mheap.go:933 +0x293 fp=0x7fff5fbff0e0 sp=0x7fff5fbff0b0
runtime.(*mheap).freeSpanLocked(0x865500, 0x905000, 0x100, 0x0)
    /home/travis/.gimme/versions/go1.6.linux.amd64/src/runtime/mheap.go:809 +0x4be fp=0x7fff5fbff148 sp=0x7fff5fbff0e0
runtime.(*mheap).grow(0x865500, 0x8, 0x0)
以下省略……

网上找了一坨坨的解决方案,都说重新安装golang1.7云云
最后,抱着试一试的态度,从golang官方下载了go1.7.3的pkg包,安装后,将$GOROOT_BOOTSTRAP指向了go1.7.3的安装目录(/usr/local/go),问题解决