(转)linux中项目部署和日志查看

1 查找进程

ps -ef | grep java   查看所有关于java的进程 

root     17540     1  0  2009 ?        01:42:27 /usr/java/jdk1.5.0_15/bin/java -Djava.util.logging.config.file=/usr/local/tomcat/conf/logging.properties -server -Xms800m -Xmx800m -XX:PermSize=64M -XX:MaxNewSize=256m -XX:MaxPermSize=128m -Djava.awt.headless=true -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -Xms256m -Xmx1024m -Djava.endorsed.dirs=/usr/local/tomcat/common/endorsed -classpath :/usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/commons-logging-api.jar -Dcatalina.base=/usr/local/tomcat -Dcatalina.home=/usr/local/tomcat -Djava.io.tmpdir=/usr/local/tomcat/temp org.apache.catalina.startup.Bootstrap start 
root     19979     1  0 Jan05 ?        00:00:51 /usr/java/latest/bin/java -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -Djava.util.logging.config.file=/usr/local/apache-tomcat-6.0.16/conf/logging.properties -Xms256m -Xmx1024m -Djava.endorsed.dirs=/usr/local/apache-tomcat-6.0.16/endorsed -classpath :/usr/local/apache-tomcat-6.0.16/bin/bootstrap.jar -Dcatalina.base=/usr/local/apache-tomcat-6.0.16 -Dcatalina.home=/usr/local/apache-tomcat-6.0.16 -Djava.io.tmpdir=/usr/local/apache-tomcat-6.0.16/temp org.apache.catalina.startup.Bootstrap start 
root     27120 27015  0 13:23 pts/4    00:00:00 grep java 

通过如下命令查看所有关于java的进程:

ps -ef | grep java 

终止线程

在得到相关进程时,将某线程终止时用 

kill -9 XXXXX 

XXXXX 为上述查出的序号  如: 19979线程终止为: kill -9 19979 

kill一个线程时需注意不要误停止了不应该停止的线程造成不必要的麻烦。 
在相当确信时才可用此方法停止线程。

输入以下命令可以查看关于tomcat的进程:

ps -ef | grep tomcat

2 查看当前服务器用户数量

w和who命令最为简单。

作为系统管理员,你可能经常会(在某个时候)需要查看系统中有哪些用户正在活动。有些时候,你甚至需要知道他(她)们正在做什么。本文为我们总结了4种查看系统用户信息(通过编号(ID))的方法。

2.1 使用w命令查看登录用户正在使用的进程信息

w命令用于显示已经登录系统的用户的名称,以及他们正在做的事。该命令所使用的信息来源于/var/run/utmp文件。w命令输出的信息包括:
用户名称
用户的机器名称或tty号
远程主机地址
用户登录系统的时间
空闲时间(作用不大)
附加到tty(终端)的进程所用的时间(JCPU时间)
当前进程所用时间(PCPU时间)
用户当前正在使用的命令
w命令还可以使用以下选项
-h忽略头文件信息
-u显示结果的加载时间
-s不显示JCPU, PCPU, 登录时间

2.2 使用who命令查看(登录)用户名称及所启动的进程

who命令用于列举出当前已登录系统的用户名称。其输出为:用户名、tty号、时间日期、主机地址。

输入以下命令,可以查看当前linux服务器在线用户数量:

who | wc -l

 

2.3 使用whoami命令查看你所使用的登录名称

whoami命令用于显示登入的用户名。

2.4 随时查看系统的历史信息(曾经使用过系统的用户信息)

last命令可用于显示特定用户登录系统的历史记录。如果没有指定任何参数,则显示所有用户的历史信息。在默认情况下,这些信息(所显示的信息)将来源于/var/log/wtmp文件。该命令的输出结果包含以下几列信息:
用户名称
tty设备号
历史登录时间日期
登出时间日期
总工作时间

可以在命令行输入“last”进行尝试。

$ last jason jason   pts/0        dev-db-server   Fri Mar 27 22:57   still logged in
jason   pts/0        dev-db-server   Fri Mar 27 22:09 - 22:54  (00:45)
jason   pts/0        dev-db-server   Wed Mar 25 19:58 - 22:26  (02:28)
jason   pts/1        dev-db-server   Mon Mar 16 20:10 - 21:44  (01:33)
jason   pts/0        192.168.201.11  Fri Mar 13 08:35 - 16:46  (08:11)
jason   pts/1        192.168.201.12  Thu Mar 12 09:03 - 09:19  (00:15)
jason   pts/0        dev-db-server   Wed Mar 11 20:11 - 20:50  (00:39

3 日志搜索

3.1 命令行查看日志

  • 首先定位要查找日志的位置
lfcp@lfcp6:~> grep -n '<TxId>1919</TxId>' *.log
GW0001.log:1094194:      <TxId>1919</TxId>
lfcp@lfcp6:~> 

可以参考:Linux命令grep

  • 再根据定位的文件和行号查看相应的日志

这里是用vi或者vim命令去查看

vim GW0001.log

#设置行号
:set nu

#根据查到的行号跳到指定的行即可

之后就可以上下移动来查看我们要搜寻的日志了。

 3.2 grep命令补充

find . -name "*.log" | xargs grep "^12405"
grep "<TxId>12426</TxId>" *.log

Linux查找文件内容的常用命令方法。
从文件内容查找匹配指定字符串的行:
$ grep "被查找的字符串" 文件名
例子:在当前目录里第一级文件夹中寻找包含指定字符串的.in文件

grep "thermcontact" */*.in

从文件内容查找与正则表达式匹配的行:

$ grep –e “正则表达式” 文件名

查找时不区分大小写:

$ grep –i "被查找的字符串" 文件名

查找匹配的行数:

$ grep -c "被查找的字符串" 文件名

从文件内容查找不匹配指定字符串的行:

$ grep –v "被查找的字符串" 文件名

从根目录开始查找所有扩展名为.log的文本文件,并找出包含”ERROR”的行

find / -type f -name "*.log" | xargs grep "ERROR"

例子:从当前目录开始查找所有扩展名为.in的文本文件,并找出包含”thermcontact”的行

find . -name "*.in" | xargs grep "thermcontact"

4 linux项目部署汇总

部署步骤:

  1. 下载jdk,tomcat
  2. 配置tomcat中的java环境变量,主要是bin目录中catalina.sh文件

export JAVA_HOME=/home/simu/jdk1.7.0_67
export JRE_HOME=/home/simu/jdk1.7.0_67/jre

       3.修改tomcat中conf目录下的server.xml文件

<?xml version='1.0' encoding='utf-8'?>
<!--
  Licensed to the Apache Software Foundation (ASF) under one or more
  contributor license agreements.  See the NOTICE file distributed with
  this work for additional information regarding copyright ownership.
  The ASF licenses this file to You under the Apache License, Version 2.0
  (the "License"); you may not use this file except in compliance with
  the License.  You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
-->
<!-- Note:  A "Server" is not itself a "Container", so you may not
     define subcomponents such as "Valves" at this level.
     Documentation at /docs/config/server.html
 -->
<Server port="11005" shutdown="SHUTDOWN">
  <Listener className="org.apache.catalina.startup.VersionLoggerListener" />
  <!-- Security listener. Documentation at /docs/config/listeners.html
  <Listener className="org.apache.catalina.security.SecurityListener" />
  -->
  <!--APR library loader. Documentation at /docs/apr.html -->
  <Listener className="org.apache.catalina.core.AprLifecycleListener" SSLEngine="on" />
  <!-- Prevent memory leaks due to use of particular java/javax APIs-->
  <Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener" />
  <Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener" />
  <Listener className="org.apache.catalina.core.ThreadLocalLeakPreventionListener" />

  <!-- Global JNDI resources
       Documentation at /docs/jndi-resources-howto.html
  -->
  <GlobalNamingResources>
    <!-- Editable user database that can also be used by
         UserDatabaseRealm to authenticate users
    -->
    <Resource name="UserDatabase" auth="Container"
              type="org.apache.catalina.UserDatabase"
              description="User database that can be updated and saved"
              factory="org.apache.catalina.users.MemoryUserDatabaseFactory"
              pathname="conf/tomcat-users.xml" />
  </GlobalNamingResources>

  <!-- A "Service" is a collection of one or more "Connectors" that share
       a single "Container" Note:  A "Service" is not itself a "Container",
       so you may not define subcomponents such as "Valves" at this level.
       Documentation at /docs/config/service.html
   -->
  <Service name="Catalina">

    <!--The connectors can use a shared executor, you can define one or more named thread pools-->
    <!--
    <Executor name="tomcatThreadPool" namePrefix="catalina-exec-"
        maxThreads="150" minSpareThreads="4"/>
    -->


    <!-- A "Connector" represents an endpoint by which requests are received
         and responses are returned. Documentation at :
         Java HTTP Connector: /docs/config/http.html (blocking & non-blocking)
         Java AJP  Connector: /docs/config/ajp.html
         APR (HTTP/AJP) Connector: /docs/apr.html
         Define a non-SSL/TLS HTTP/1.1 Connector on port 8080
    -->
    <Connector port="8090" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="11443" />
    <!-- A "Connector" using the shared thread pool-->
    <!--
    <Connector executor="tomcatThreadPool"
               port="8080" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8443" />
    -->
    <!-- Define a SSL/TLS HTTP/1.1 Connector on port 8443
         This connector uses the NIO implementation that requires the JSSE
         style configuration. When using the APR/native implementation, the
         OpenSSL style configuration is required as described in the APR/native
         documentation -->
    <!--
    <Connector port="8443" protocol="org.apache.coyote.http11.Http11NioProtocol"
               maxThreads="150" SSLEnabled="true" scheme="https" secure="true"
               clientAuth="false" sslProtocol="TLS" />
    -->

    <!-- Define an AJP 1.3 Connector on port 8009 -->
    <Connector port="11007" protocol="AJP/1.3" redirectPort="11443" />


    <!-- An Engine represents the entry point (within Catalina) that processes
         every request.  The Engine implementation for Tomcat stand alone
         analyzes the HTTP headers included with the request, and passes them
         on to the appropriate Host (virtual host).
         Documentation at /docs/config/engine.html -->

    <!-- You should set jvmRoute to support load-balancing via AJP ie :
    <Engine name="Catalina" defaultHost="localhost" jvmRoute="jvm1">
    -->
    <Engine name="Catalina" defaultHost="localhost">

      <!--For clustering, please take a look at documentation at:
          /docs/cluster-howto.html  (simple how to)
          /docs/config/cluster.html (reference documentation) -->
      <!--
      <Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"/>
      -->

      <!-- Use the LockOutRealm to prevent attempts to guess user passwords
           via a brute-force attack -->
      <Realm className="org.apache.catalina.realm.LockOutRealm">
        <!-- This Realm uses the UserDatabase configured in the global JNDI
             resources under the key "UserDatabase".  Any edits
             that are performed against this UserDatabase are immediately
             available for use by the Realm.  -->
        <Realm className="org.apache.catalina.realm.UserDatabaseRealm"
               resourceName="UserDatabase"/>
      </Realm>

      <Host name="localhost"  appBase="webapps"
            unpackWARs="true" autoDeploy="true">
    <Context path="simuflcpweb" debug="0" docBase="/home/simu/service" reloadable="true"></Context>
        <!-- SingleSignOn valve, share authentication between web applications
             Documentation at: /docs/config/valve.html -->
        <!--
        <Valve className="org.apache.catalina.authenticator.SingleSignOn" />
        -->

        <!-- Access log processes all example.
             Documentation at: /docs/config/valve.html
             Note: The pattern used is equivalent to using pattern="common" -->
        <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
               prefix="localhost_access_log" suffix=".txt"
               pattern="%h %l %u %t &quot;%r&quot; %s %b" />

      </Host>
    </Engine>
  </Service>
</Server>

4.1 跨用户复制文件

前言

考虑如下情景:foo用户home目录下有一文件file.txt,要将其copy至bar用户的home目录。Linux对用户home目录有严格的权限限制,非owner用户或者同group用户无权限读写,除非是root(至高无上的root)。如果没有root权限,有什么办法把file.txt 复制到bar用户的home目录下呢?

想到两个办法

第一个办法,先用foo用户登录,把文件copy到系统临时目录/tmp,然后切换到bar用户,再从系统临时目录/tmp把文件copy到自己的home目录。这里为什么用cp不用mv?因为复制到/tmp的文件owner还是foo,默认情况下其他用户自有读权限,没有写权限(自然没有移动权限)。即使通过修改文件权限,让bar可写,移动到bar的home目录下owner还是foo,而且非得root才能改成bar。这个办法有点曲折,弊端也很明显,文件需要复制两次,花两倍的时间。

# cp file.txt /tmp/ 
# su - bar 
# cp /tmp/file.txt ~/ 
# exit 
# rm /tmp/file.txt

第二个办法,使用scp命令。原本scp是用来在不同主机上通过网络copy文件,用在这里刚好。用bar用户登录

# scp foo@localhost:/home/foo/file.txt ./

输入foo用户密码,开始文件传输。

也可以用foo用户登录,

# scp file.txt bar@localhost:/home/bar/ 
输入bar用户密码,过程一样。
 实际中使用哪一种方法,看个人喜好了。如果要copy的文件不大,第一种方法也不失为一种选择。

下面采用第二种方法操作一遍:

simu@LFCP-6:~> ll
总用量 32
-rw------- 1 simu mqm 4546  2月  7 17:42 application.properties
-rw------- 1 simu mqm 2032  2月  7 17:42 autoDeploy.sh
drwxr-xr-x 2 simu mqm 4096 12月 16 21:23 bin
drwx------ 2 simu mqm 4096  2月  7 17:42 key
-rw------- 1 simu mqm   57  2月  7 17:42 log.sh
-rw------- 1 simu mqm   49  2月  7 17:42 startup.sh
-rw------- 1 simu mqm   61  2月  7 17:42 stop.sh
simu@LFCP-6:~> scp lfcp@localhost:/home/lfcp/simu/jdk1.7.0_67.tar ./
The authenticity of host 'localhost (127.0.0.1)' can't be established.
ECDSA key fingerprint is 0c:99:38:31:34:80:c3:51:ea:9d:97:b7:3d:8c:48:ad [MD5].
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'localhost' (ECDSA) to the list of known hosts.
Password: 
jdk1.7.0_67.tar                                                                                                                   100%  282MB  93.9MB/s   00:03    
simu@LFCP-6:~> ll
总用量 288672
-rw------- 1 simu mqm      4546  2月  7 17:42 application.properties
-rw------- 1 simu mqm      2032  2月  7 17:42 autoDeploy.sh
drwxr-xr-x 2 simu mqm      4096 12月 16 21:23 bin
-rw------- 1 simu mqm 295270400  2月  7 17:51 jdk1.7.0_67.tar
drwx------ 2 simu mqm      4096  2月  7 17:42 key
-rw------- 1 simu mqm        57  2月  7 17:42 log.sh
-rw------- 1 simu mqm        49  2月  7 17:42 startup.sh
-rw------- 1 simu mqm        61  2月  7 17:42 stop.sh
simu@LFCP-6:~> 

5 项目部署问题

5.1 基本问题

  • 问题 ——编辑配置文件,出现隐藏文件提示

在编辑tomcat的配置文件catalina.sh 时候提示发现配置文件。

这是因为,在用vim打开一个文件时,其会产生一个filename.swap文件,用于保存数据,当文件非正常关闭时,可用此文件来恢复,当正常关闭时,此文件会被删除,非正常关闭时,不会被删除,所以提示存在.swap文件,此时你可以恢复文件:
恢复以后把.swap文件删掉,在打开时就不会用提示良,注意.swap文件是个隐藏文件。可用:la查看。以.开头的是隐藏文件。
在linux下隐藏文件是以“.”开头的,单纯的使用ls命令是看不到的,加上“-a”参数才可以。删除则可以使用命令:rm -fr .*(删除当前目录下的所有隐藏文件), rm -f .tmp(删除tmp文件),rm -fr .tmp(删除tmp目录或者文件)
  •  问题——打印日志提示权限不够

这里的tomcat和jdk都直接解压而来,在输出catalina.out 日志时候,提示没有权限,实际测试时候发现相应的进程也没有启动。

 使用以下方式给jdk整个目录授权

chmod -R 777 /home/simu/jdk1.7.0_67

可以通过查看进程的方式查看tomcat是否启动:

ps -ef | grep simu(进程的名字)

如果还不能够启动tomcat,尝试删除tomcat中的原有日志catalina.out.

w

原文地址:https://www.cnblogs.com/lixuwu/p/5854985.html