MACOS 常用命令

macos 常用命令

1.brew

  • ⚠️:使用sudo 和不使用时不一样的「使用sudo和不使用看到的是不一样的」

  • sudo brew install 应用名称

#例子: 安装redis
sudo brew install redis@6.2 #后面@接版本号可指定版本
  • sudo brew services start 服务名称

#启动服务「开机会自动启动服务」
sudo brew services start redis
  • sudo brew services stop 服务名称

#停止服务「开机也不会自动启动」
sudo brew services stop redis
  • sudo brew services restart 服务名称

#重新启动服务
sudo brew services restart redis
  • sudo brew services list 查看服务列表

sudo brew services list
  • sudo brew uninstall 服务名

#卸载服务
sudo brew uninstall redis
  • brew info nginx-full

#查看 服务配置信息
#查看 配置文件位置
brew info nginx-full |grep conf
#查看 网页位置
brew info nginx-full |grep Docroot

2. launchctl

  • ⚠️:使用sudo 和不使用时不一样的「使用sudo和不使用看到的是不一样的」

  • sudo launchctl list

#列出所有服务 
#注意服务的名称比较长,里面可以包含 / - . 等
sudo launchctl list 
sudo  launchctl list |grep httpd
  • sudo launchctl print system/org.apache.httpd

#可能会提示使用全名显示服务详细信息
# system/homebrew.mxcl.nginx-full 整个是服务名称
#其中有state 显示 服务目前的状态,例如: running、但停止了就不能用print显示了,list也看不到了
sudo launchctl print system/homebrew.mxcl.nginx-full
  • sudo launchctl start

#启动一个服务
sudo launchctl start system/homebrew.mxcl.nginx-full
  • launchctl stop

在 macOS 中,launchctl stop 是一个用于控制 LaunchDaemons 或 LaunchAgents 的命令,它的作用是 向指定的服务(job)发送停止信号,要求其停止运行。
具体功能:
停止服务:launchctl stop 会通知指定的服务(由 label 标识)停止运行,但不会从 launchd 的管理列表中移除(与 unload 不同)。
依赖 launchd:服务是否真的停止取决于它是否遵守 launchd 的信号,某些服务可能会自动重启(如果配置了 KeepAlive 选项)。
#停止一个服务
#不成功也不会显示任何信息,需要使用:
sudo launchctl stop system/org.apache.httpd
sudo echo $?
#system/org.apache.httpd 需要使用如下命令停止:
sudo apachectl stop
  • launchctl load

  • macos新版本sudo launchctl bootstrap system /System/Library/LaunchDaemons/org.apache.httpd.plist

  • 使用服务路径全名,运行后服务就启动了

# 加载服务配置文件
sudo launchctl load system/org.apache.httpd
  • launchctl unload

  • macos新版本使用 sudo launchctl bootout system/org.apache.httpd

  • bootout后launchctl就看不到了,并且自动停止了服务

# 卸载服务配置文件
sudo launchctl unload system/org.apache.httpd
#卸载后不在启动列表,但.list 文件还存在:
sudo ls /Library/LaunchDaemons/      # 系统级
sudo ls ~/Library/LaunchAgents/      # 用户级
sudo ls /System/Library/LaunchDaemons/

3. 端口进程

  • 查看占用端口的进程id:sudo lsof -i :80

  • 根据进程id查看进程文件名:sudo lsof -p 98674

# macos 常用命令

## 1.brew

- ⚠️:使用sudo 和不使用时不一样的「使用sudo和不使用看到的是不一样的」
- sudo brew install 应用名称

```bash
#例子: 安装redis
sudo brew install redis@6.2 #后面@接版本号可指定版本
```

- sudo brew services start 服务名称

```bash
#启动服务「开机会自动启动服务」
sudo brew services start redis
```

- sudo brew services stop 服务名称

```bash
#停止服务「开机也不会自动启动」
sudo brew services stop redis
```

- sudo brew services restart 服务名称

```bash
#重新启动服务
sudo brew services restart redis
```

- sudo brew services list 查看服务列表

```bash
sudo brew services list
```

- sudo brew uninstall 服务名

```bash
#卸载服务
sudo brew uninstall redis
```

- brew info nginx-full

```bash
#查看 服务配置信息
#查看 配置文件位置
brew info nginx-full |grep conf
#查看 网页位置
brew info nginx-full |grep Docroot
```

## 2. launchctl

- ⚠️:使用sudo 和不使用时不一样的「使用sudo和不使用看到的是不一样的」
- sudo launchctl list

```bash
#列出所有服务 
#注意服务的名称比较长,里面可以包含 / - . 等
sudo launchctl list 
sudo  launchctl list |grep httpd
```

- sudo launchctl print system/org.apache.httpd

```bash
#可能会提示使用全名显示服务详细信息
# system/homebrew.mxcl.nginx-full 整个是服务名称
#其中有state 显示 服务目前的状态,例如: running、但停止了就不能用print显示了,list也看不到了
sudo launchctl print system/homebrew.mxcl.nginx-full
```

- sudo launchctl start 

```bash
#启动一个服务
sudo launchctl start system/homebrew.mxcl.nginx-full
```

- launchctl stop 

```bash
在 macOS 中,launchctl stop 是一个用于控制 LaunchDaemons 或 LaunchAgents 的命令,它的作用是 向指定的服务(job)发送停止信号,要求其停止运行。
具体功能:
停止服务:launchctl stop 会通知指定的服务(由 label 标识)停止运行,但不会从 launchd 的管理列表中移除(与 unload 不同)。
依赖 launchd:服务是否真的停止取决于它是否遵守 launchd 的信号,某些服务可能会自动重启(如果配置了 KeepAlive 选项)。
#停止一个服务
#不成功也不会显示任何信息,需要使用:
sudo launchctl stop system/org.apache.httpd
sudo echo $?
#system/org.apache.httpd 需要使用如下命令停止:
sudo apachectl stop
```

- launchctl load 
- macos新版本sudo launchctl bootstrap system /System/Library/LaunchDaemons/org.apache.httpd.plist
- 使用服务路径全名,运行后服务就启动了

```bash
# 加载服务配置文件
sudo launchctl load system/org.apache.httpd
```

- launchctl unload 
- macos新版本使用 sudo launchctl bootout  system/org.apache.httpd
- bootout后launchctl就看不到了,并且自动停止了服务

```bash
# 卸载服务配置文件
sudo launchctl unload system/org.apache.httpd
#卸载后不在启动列表,但.list 文件还存在:
sudo ls /Library/LaunchDaemons/      # 系统级
sudo ls ~/Library/LaunchAgents/      # 用户级
sudo ls /System/Library/LaunchDaemons/
```

## 3. 端口进程

- 查看占用端口的进程id:sudo lsof -i :80
- 根据进程id查看进程文件名:sudo lsof -p 98674