Add crontab to Linux to regularly clean up Oracle long-time inactive session

Time:2021-9-26

Add crontab to Linux to regularly clean up Oracle long-time inactive session

if [ ! -d "${HOME}/cront/" ];then
mkdir ${HOME}/cront
else
echo "Do nothing"
fi

# ${HOME}/cront/ks.sql
echo "set trimspool on
spool ${HOME}/cront/kills.sh replace
set pagesize 0
set heading off
set linesize 32767
SELECT 'kill -9 ' || P.SPID FROM V$PROCESS P WHERE P.ADDR IN (SELECT B.PADDR FROM V$SESSION B WHERE B.TYPE = 'USER' AND B.STATUS = 'INACTIVE' AND B.LAST_CALL_ET > 3600);
exit;
spool off" > ${HOME}/cront/ks.sql

# ${HOME}/cront/debug.sql
echo "oradebug wakeup 2;
exit;" > ${HOME}/cront/debug.sql

# ${HOME}/cront/ks.sh
echo '#!/bin/bash' > ${HOME}/cront/ks.sh
echo '' > ${HOME}/cront/cron_kill.log
echo '' > ${HOME}/cront/kill.sh
echo '' > ${HOME}/cront/kills.sh
echo "source ${HOME}/.bash_profile
sqlplus / as sysdba @${HOME}/cront/ks.sql
grep \"kill\" ${HOME}/cront/kills.sh > ${HOME}/cront/kill.sh
echo -e >> ${HOME}/cront/cron_kill.log
echo \"################# Started 'kill -9' commend at \`date +'%F %T %A'\` ##################\" > ${HOME}/cront/cron_kill.log
echo -e >> ${HOME}/cront/cron_kill.log
# Kill session
${HOME}/cront/kill.sh >> ${HOME}/cront/cron_kill.log

# oradebug
echo \"# oradebug , No Information Return\" >> ${HOME}/cront/cron_kill.log
sqlplus / as sysdba @${HOME}/cront/debug.sql

# Info
echo -e >> ${HOME}/cront/cron_kill.log
echo \"Warning: \`cat ${HOME}/cront/kill.sh | wc -l\` processes Killed. \" >> ${HOME}/cront/cron_kill.log
echo -e >> ${HOME}/cront/cron_kill.log 
echo \"################  Stoped at \`date +'%F %T'\` ################\" >> ${HOME}/cront/cron_kill.log
" >> ${HOME}/cront/ks.sh

# Grant execute permission
chmod +x ${HOME}/cront/*

# Add Crontd
(crontab -l | grep -v "ks.sh") | crontab
(crontab -l ; echo "*/30 * * * * /bin/bash ${HOME}/cront/ks.sh") | crontab

Recommended Today

Supervisor

Supervisor [note] Supervisor – H view supervisor command help Supervisorctl – H view supervisorctl command help Supervisorctl help view the action command of supervisorctl Supervisorctl help any action to view the use of this action 1. Introduction Supervisor is a process control system. Generally speaking, it can monitor your process. If the process exits abnormally, […]