| 155 | |
| 156 | # 执行启动服务并返回PID信息或错误信息 |
| 157 | def start(self) -> Optional[int]: |
| 158 | if not self.env: |
| 159 | raise RuntimeError('未设置环境') |
| 160 | log_file = os.path.join(SERVICE_PATH, '{}/{}.log'.format(self.env.project_name, self.name)) |
| 161 | pid_file = os.path.join(SERVICE_PATH, '{}/{}.pid'.format(self.env.project_name, self.name)) |
| 162 | if not os.path.exists(os.path.dirname(pid_file)): |
| 163 | os.makedirs(os.path.dirname(pid_file), 0o755) |
| 164 | if os.path.exists(pid_file): |
| 165 | os.remove(pid_file) |
| 166 | prep_sh = self.env.shell_env() |
| 167 | prep_sh += "\nexport BT_PYTHON_SERVICE_SID={}".format(self.sid) |
| 168 | if not os.path.isfile(log_file): |
| 169 | public.writeFile(log_file, '') |
| 170 | public.set_own(log_file, self.env.user) |
| 171 | public.set_mode(log_file, "755") |
| 172 | if self.log_type == "append": |
| 173 | prep_sh += "\nnohup {} &>> {} &".format(self.command, log_file) |
| 174 | else: |
| 175 | prep_sh += "\nnohup {} &> {} &".format(self.command, log_file) |
| 176 | |
| 177 | public.print_log(prep_sh) |
| 178 | res = public.ExecShell(prep_sh, user=self.env.user) |
| 179 | public.print_log(res) |
| 180 | time.sleep(0.5) |
| 181 | return self.get_service_pid() |
| 182 | |
| 183 | def get_service_pid(self, only_service: bool = False) -> Optional[int]: |
| 184 | pid = self.read_pid() |