在 CentOS 中“导入脚本”通常有以下几种常见场景,我分别说明:
例如有一个脚本 test.sh:
#!/bin/bash
echo "Hello CentOS"
chmod +x test.sh
./test.sh
或
bash test.sh
source(推荐)source ./other.sh
或简写:
. ./other.sh
✅ 特点:
other.sh
#!/bin/bash
say_hello() {
echo "Hello"
}
main.sh
#!/bin/bash
source ./other.sh
say_hello
vi /etc/rc.d/rc.local
添加:
/root/test.sh
赋权:
chmod +x /etc/rc.d/rc.local
vi /etc/systemd/system/myscript.service
内容:
[Unit]
Description=My Script
[Service]
ExecStart=/root/test.sh
[Install]
WantedBy=multi-user.target
启用:
systemctl daemon-reload
systemctl enable myscript.service
systemctl start myscript.service
crontab -e
添加:
* * * * * /root/test.sh
scp test.sh root@centos_ip:/root/
yum install -y lrzsz
rz # 上传
如果你说的“导入脚本”是指:
可以告诉我具体场景,我可以给你更精确的命令。