这里我是使用的我另一篇文章中的function的方法,把代码加到环境变量中。
一、函数脚本
主要功能命令分为qsub
、rm
、get
...(后面还可以再加别的),这些参数后面还可以加其他的参数变量,在脚本中进行解析。调用就是sge_tools 主命令 参数...
主命令 | 功能 |
---|---|
qsub | 批量提交shell脚本任务 |
rm | 删除/只保留,全部/部分qsub测试完成后目录下的.e .o文件 |
get | 获取qstat命令下对应的任务ID的shell脚本信息 |
首先放上脚本,如下代码可以直接拷贝到.bashrc
或者其他环境变量中,然后source即可使用。
sge_tools ()
{
# 帮助信息
if [ -z "$1" ] || [ "$1" = "-h" ] ;then
cat <<HELP
---------------------------------------------------------------
Author: myshu
Mail: myshu0601@qq.com
Version: 1.0
Date: 2024-7-20
Description: sge qsub和qstat等相关批量操作
---------------------------------------------------------------
USAGE: $0
or $0 -h # show this message
1、qsub [shell_file] [vf_arg] # 批量输出提交指定的shell命令
eg:qsub test*.sh "-P testprj -q bc.q -l num_proc=1,vf=10G" # 表示只输出qsub命令,如果要提交,直接 qsub test*.sh | sh 即可
2、rm [-a] [-e] [shell_file] [qsub_id] # 按照规则删除/保留指定的所有.e .o文件
-a 表示all,即删除指定的所有.e .o文件
-e 表示exclude,即保留指定任务id的.e .o文件,其余全部删除
-a和-e两者只能选一个
eg:
-a test.sh "2350939 2351273" # 删除指定的所有的test.sh.*2350939和test.sh.*2351273
-a test.sh # 删除指定的所有的test.sh.*
-e test.sh "2350939 2351273" # 删除除了test.sh.*2350939和test.sh.*2351273之外的其他test.sh.*
3、get [top_n] # 获取qstat前n个任务的ID 和 脚本提交路径
eg: get 10
HELP
return 0
fi
# --- 批量提交qsub任务
if [ $1 == "qsub" ];then
ls $2 | xargs -i echo qsub -wd $3 {}
fi
# -- 删除指定脚本的所有id的 .o .e文件
if [ $1 == "rm" ];then
if [ $2 == "-a" ];then
if [ -n "$4" ];then
rm_ids=`echo $4 | sed "s/^/*/g;s/ / */g"`
rm $rm_ids
else
rm $3.*
fi
return 1
fi
# 排除指定的id ,删除脚本其他的id
if [ $2 == "-e" ];then
save_ids=`echo $4 | sed "s/ /|/g"`
ls $3.* | grep -vE "$save_ids" | xargs rm
return 1
fi
fi
# --- 获取qstat前n个任务的ID 和 脚本提交路径
if [ $1 == "get" ];then
num=$2
for i in `qstat |sed -e 's/^[ ]*//g' | cut -f 1 -d " " | sed -n "3,${num}p" `
do
echo $i | tr '\n' '\t'
scp=`qstat -j $i | grep "script_file" `
scp_new=${scp:28}
name=$(basename $scp_new)
echo $name | tr '\n' '\t'
#echo $name
#echo -e "\t$scp_new"
echo "$scp_new"
done
fi
}
二、使用
source 环境之后,就可以直接使用快捷键sge_tools
运行命令。
# qsub [shell_file] [vf_arg] # 批量输出提交指定的shell命令
sge_tools qsub test*.sh "-P testprj -q bc.q -l num_proc=1,vf=10G" # 只输出qsub命令,如果要提交,直接 命令 | sh 即可
# sge_tools rm [-a] [-e] [shell_file] [qsub_id] # 按照规则删除/保留指定的所有.e .o文件
sge_tools rm -a test.sh "2350939 2351273" # 删除指定的所有的test.sh.*2350939和test.sh.*2351273
sge_tools rm -a test.sh # 删除指定的所有的test.sh.*
sge_tools rm -e test.sh "2350939 2351273" # 删除除了test.sh.*2350939和test.sh.*2351273之外的其他test.sh.*
# get [top_n] # 获取qstat前n个任务的ID 和 脚本提交路径
sge_tools get 10
其实其他的日常常用的一些串联的命令或者处理都可以写成function的形式存在我们自己的bashrc中,针对性很强,而且方便自己使用,相当顺手~
网友评论