liunx shell命令基础

liunx shell命令基础

1、执行shell脚本的命令

shell脚本执行需要 x权限

./xxx.sh   

[root@hadoop01 ~]# vi test.log   #创建shell脚本,打印时间

#!/bin/bash   声明#!/bin/bash 解释器

date    命令行

[root@hadoop01 ~]# ./test.log

-bash: ./test.log: 权限不够

[root@hadoop01 ~]# ll test.log

-rw-r--r--. 1 root root 61 11月 22 11:22 test.log #没有执行的权限,需要授权。

[root@hadoop01 ~]# chmod 744 test.log   #授权

[root@hadoop01 ~]# ll test.log

-rwxr--r--. 1 root root 61 11月 22 11:22 test.log  

[root@hadoop01 ~]# ./test.log  

2019年 11月 22日 星期五 11:50:10 CST   #执行成功

sh ./xxx.sh

[root@hadoop01 ~]# sh ./test.log

2019年 11月 22日 星期五 11:22:21 CST

使用linux 自带crontab调度工具执行shell脚本,并将结果输出到日志文件

[root@hadoop01 ~]# cp test.log test.sh  #拷贝test.log并更名为test.sh

[root@hadoop01 ~]# crontab -e

* * * * * /root/test.sh >> /root/test.log #每分钟执行test.sh脚本,将结果写入到test.log文件

* * * * * #这5个*从左往右分别表达的意思如下:

小时

保存后系统会自动运行。

[root@hadoop01 ~]# tail -F test.log  #实时查看test.log文件

2019年 11月 22日 星期五 12:11:01 CST

2019年 11月 22日 星期五 12:12:01 CST

2019年 11月 22日 星期五 12:13:01 CST

2019年 11月 22日 星期五 12:14:01 CST

2019年 11月 22日 星期五 12:15:01 CST

面试题:每隔10秒执行一次test1.sh文件,将结果写入到test.log文件。如何实现?

[root@hadoop01 ~]# vi test1.sh  创建test1.sh文件,输入命令行

#!/bin/bash

for((i=1;i<=6;i++)); #for循环,拆分成6次循环

do

        date #执行命令

        sleep 10s #执行后休眠10秒

done

exit #退出

~       

:wq 保存退出

[root@hadoop01 ~]# tail -F test.log  #实时查看test.log文件

2019年 11月 22日 星期五 12:54:01 CST

2019年 11月 22日 星期五 12:54:11 CST

2019年 11月 22日 星期五 12:54:21 CST

2019年 11月 22日 星期五 12:54:31 CST

后台执行的方法

第一种:./test.sh &


第二种:nohup ./test.sh &  #用于手动启动脚本 看日志 开发维护 测试

[root@hadoop01 ~]# nohup ./test.sh &

[1] 100648

[root@hadoop01 ~]# nohup: 忽略输入并把输出追加到"nohup.out"

[root@hadoop01 ~]# cat nohup.out 

2019年 11月 22日 星期五 13:02:14 CST

第三种:nohup ./test.sh > /root/test.log 2>&1 &  生产上使用

[root@hadoop01 ~]# nohup ./test1.sh > /root/test2.log 2>&1 & #执行shell脚本,并将结果输出到指定的文件,>符号是覆盖或新增的意思,>>符号是追加的意思。

[1] 111450

[root@hadoop01 ~]# cat test2.log

nohup: 忽略输入

2019年 11月 22日 星期五 13:06:27 CST

2019年 11月 22日 星期五 13:06:37 CST

ln -s软链接的使用方法。

使用场景:软件版本升级,源文件移动

命令格式:ln -s 原path 目标path

[root@hadoop01 ~]# ln -s mysql-5.6.23-linux-glibc2.5-x86_64 mysql #创建mysql的软链接

[mysqladmin@hadoop01 local]$ ll /usr/local/*mysql

lrwxrwxrwx. 1 mysqladmin dba 34 11月 21 19:37 /usr/local/mysql -> mysql-5.6.23-linux-glibc2.5-x86_64

原文件丢失软链接光标跳动的解决方法:

  1.找原文件文件夹;

  2. 删除快捷键文件文件夹,重新做新的软连接 高版本。

注意:创建软链接很容易忽略:权限的问题,建议查看软链接和源文件的权限并修改。

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容