In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly explains the "execute linux commands to clean up the server cache and return results", the content of the article is simple and clear, easy to learn and understand, now please follow the editor's ideas slowly in depth, together to study and learn "execute linux commands to clean the server cache and return results how to achieve" it!
1. Prepare the environment idea spingboot 2.0
Org.springframework.boot spring-boot-starter org.springframework.boot spring-boot-devtools runtime true org.apache.commons commons-lang3 3.3.2 ch.ethz.ganymed ganymed-ssh3 build210 org.projectlombok lombok true org.springframework.boot Spring-boot-starter-test test org.junit.vintage junit-vintage-engine
two。 Create a Boot startup class
@ SpringBootApplication// starts the timing task @ EnableSchedulingpublic class SshtimedtaskApplication {public static void main (String [] args) {SpringApplication.run (SshtimedtaskApplication.class, args);}}
3. Create a logical class
Directory structuring
Inject scheduled tasks into the container according to spring ioc, and start it as soon as it starts
@ Component@Slf4jpublic class TimedTaskConfig {@ Resource TimedTaskService timedTaskService; / / "0 15 10? * *" the test successfully triggers @ Scheduled (cron= "023 * *?) at 10:15 every morning / / triggers public void clearLinuxProcess () {log.info (" get the remote server process number: "+ new Date ()); String proceStr=timedTaskService.execGrepResult () at 23:00 every night. / / String pro [] = proceStr.split (""); log.info ("execute remote server to end the process kill-9" + proceStr); timedTaskService.execKileProcess (proceStr);}}
TimedTaskService timing task logic class
@ Slf4j@Servicepublic class TimedTaskService {/ / ps-ef | grep xxx.log | grep-v grep | awk'{print $2} 'query the process number of a process on the server this is the process number of a push stream private static final String execFindGrep= "ps-aux | grep rtsp_transport | grep-v grep | awk' {print $2}'"; private static final String killProcess= "kill-9" Public String execGrepResult () {log.info ("ready to execute command:" + execFindGrep); return ScpSshConnectionClient.execute (execFindGrep); / / process number returned by query} public void execKileProcess (String cmd) {log.info ("ready to execute command:" + killProcess+cmd); ScpSshConnectionClient.execute (killProcess+cmd); / / batch processing}}
Ssh connects to the server
/ * ClassName:ScpSshConnectionClient * Function: TODO ADD java operation to upload and download files on scp. * Reason: related to TODO ADD server. * Date: 9:55:43 * @ author wangmeng * @ version * @ since JDK 1.8 * @ see * / @ Slf4jpublic class ScpSshConnectionClient {private static final String IP= "172.XXXXXXXX"; / / remote server address private static final String USR= "XXXX"; / / remote server user name private static final String PSWORD= "XXXXXXXX" / / remote server password private static String DEFAULTCHART= "UTF-8"; private static Connection connection = new Connection (IP); / / create object / * login:ssh user login authentication, using username and password to authenticate. * @ author wangmeng * @ return boolean * @ since JDK 1.8 * / public static boolean login () {/ / create a remote connection. The default connection port is 22. If you do not use the default, you can use the method try {connection.connect (); / / use the user name and password to log in with the key and use authenticateWithPublicKey to authenticate return connection.authenticateWithPassword (USR,PSWORD). } catch (IOException e) {log.error ("user s password s login server s failed!" , USR,PSWORD,IP,e);} return false;} / * remote execution of the shll script or command * @ param cmd * the result value returned after the command to be executed * @ return * is returned * / public static String execute (String cmd) {String result= "; try {boolean isAuthed = login () If (isAuthed & & connection! = null) {Session session= connection.openSession (); / / Open a session session.execCommand (cmd); / / execute the command result=processStdout (session.getStdout (), DEFAULTCHART) / / if the if (StringUtils.isBlank (result)) {log.info ("get the standard output is empty, link connection:" + connection+ ", the command executed is" + cmd); result=processStdout (session.getStderr (), DEFAULTCHART) " } else {log.info ("execute command successfully, link connection:" + connection+ ", command executed:" + cmd);} connection.close (); session.close ();}} catch (IOException e) {log.error ("failed to execute command, link connection:" + connection+ ", command executed:" + cmd+ "+ e.getMessage ()) E.printStackTrace ();} return result;} / * parse the result set returned by the script execution * @ param in input stream object * @ param charset encoding * @ return * return * / private static String processStdout (InputStream in, String charset) {InputStream stdout = new StreamGobbler (in) in plain text format StringBuffer buffer = new StringBuffer ();; try {BufferedReader br = new BufferedReader (new InputStreamReader (stdout,charset)); String line=null; while ((line=br.readLine ())! = null) {buffer.append (line+ ");}} catch (UnsupportedEncodingException e) {log.error (" parsing script error: "+ e.getMessage ()) E.printStackTrace ();} catch (IOException e) {log.error ("parsing script error:" + e.getMessage ()); e.printStackTrace ();} return buffer.toString () }} Thank you for your reading. The above is the content of "executing the linux command to clean up the server cache and return the result". After the study of this article, I believe you have a deeper understanding of how to execute the linux command to clean the server cache and return the result, and the specific usage needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.