用于从pgrep grunt中杀死PID的bash脚本

时间:2017-03-09 13:34:53

标签: linux bash shell

我是一个打击脚本的全明星菜鸟。我的脚本看起来像这样:

#!/bin/bash 

pgrep grunt > target.txt  
for i in target.txt  
   do kill $i
   echo "killed $i"  
done

现在,显然它没有用,我不知道如何使它工作。 我希望此脚本echo pgrep grunt输出到文件 target.txt ,并从那里读取PID并终止这些进程。

3 个答案:

答案 0 :(得分:1)

回答你的问题:

#!/bin/bash

while read i; do 
   kill $i
   echo "killed $i"
done< <(pgrep grunt)

不需要外部文件

但是,你知道pkill存在吗?

 pkill grunt
  

pgrep,pkill - 根据姓名等查找或发出信号流程   属性

答案 1 :(得分:0)

使用bash脚本读取文件

#!/bin/bash
while IFS='' read -r line || [[ -n "$line" ]]; 
do 
    kill $line
    echo "killed $line"
done < target.txt

答案 2 :(得分:0)

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.RemoteException;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import java.io.IOException;

import com.honeywell.decodemanager.DecodeManager;
import com.honeywell.decodemanager.SymbologyConfigs;
import com.honeywell.decodemanager.barcode.DecodeResult;
import com.honeywell.decodemanager.symbologyconfig.SymbologyConfigCode39;


public final class MainActivity extends Activity {

    private final int ID_SCANSETTING = 0x12;
    private final int ID_CLEAR_SCREEN = 0x13;
    private final int SCANKEY        = 0x94;
    private DecodeManager mDecodeManager = null;
    private TextView mDecodeResultEdit = null;
    private final int SCANTIMEOUT = 2000;
    long mScanAccount = 0;
    private boolean mbKeyDown = true;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        final Button button = (Button) findViewById(R.id.button);
        mDecodeResultEdit = (TextView) findViewById(R.id.textView);
        button.setOnTouchListener(new Button.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                // TODO Auto-generated method stub
                final int action = event.getAction();
                switch (action) {
                    case MotionEvent.ACTION_DOWN:
                        try {
                            if (mbKeyDown) {
                                DoScan();
                                mbKeyDown = false;
                            }
                        } catch (Exception e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                        break;
                    case MotionEvent.ACTION_UP:
                        try {
                            mbKeyDown = true;
                            cancleScan();
                        } catch (Exception e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                        break;
                }
                return true;
            }
        });
    }
        private void DoScan() throws Exception {
            try {
                mDecodeManager.doDecode(SCANTIMEOUT);
            } catch (RemoteException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

    @Override
    protected void onResume() {
        super.onResume();

        if (mDecodeManager == null) {
            mDecodeManager = new DecodeManager(this ,ScanResultHandler);
        }
    }

    @Override
    protected void onPause() {
        super.onPause();

        if (mDecodeManager != null) {
            try {
                mDecodeManager.release();
                mDecodeManager = null;
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    private Handler ScanResultHandler = new Handler() {
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case DecodeManager.MESSAGE_DECODER_COMPLETE:
                    String strDecodeResult = "";
                    DecodeResult decodeResult = (DecodeResult) msg.obj;
                    strDecodeResult = "Decode Result::"+ decodeResult.barcodeData;

                    mDecodeResultEdit.setText(strDecodeResult);
                    break;

                case DecodeManager.MESSAGE_DECODER_FAIL:
                    mDecodeResultEdit.setText("FAILED");
                    break;

                case DecodeManager.MESSAGE_DECODER_READY:
                {
                    try {
                        SymbologyConfigCode39 code39 = new SymbologyConfigCode39();
                        code39.enableCheckEnable(false);
                        code39.enableSymbology(false);
                        code39.setMaxLength(48);
                        code39.setMinLength(2);

                        SymbologyConfigs symconfig = new SymbologyConfigs();

                        symconfig.addSymbologyConfig(code39);
                        mDecodeManager.setSymbologyConfigs(symconfig);

                    } catch (RemoteException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
                    break;

                default:
                    super.handleMessage(msg);
                    break;
            }
        }
    };

    private void cancleScan() throws Exception {
        mDecodeManager.cancelDecode();
    }
}