启动tmux会话,该会话在运行脚本后未关闭

问题描述

如何使用脚本(例如)创建tmux会话

class AnotherStatefulWidget extends StatefulWidget {
  final List<String> items;

  AnotherStatefulWidget(this.items);

  @override
  _AnotherStatefulWidgetState createState() => _AnotherStatefulWidgetState();
}

class _AnotherStatefulWidgetState extends State<AnotherStatefulWidget> {
  final ScrollController scrollController = ScrollController();
  ItemsBloc _itemsBloc = getit<ItemsBloc>();

  bool _handleNotification(ScrollNotification notification,List<String> items) {
    if (notification is ScrollEndNotification &&
        scrollController.position.extentAfter == 0.00) {
      _itemsBloc.add(ItemsLoadEvent.loadMoreItems(
          categories: items,document: ...));
    }

    return false;
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.black,body: Container(
        child: Center(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,children: <Widget>[
              Container(
                width: double.infinity,height: 280,child: Padding(
                  padding: EdgeInsets.only(
                    right: 8,),child: NotificationListener<ScrollNotification>(
                    onNotification: (_n) =>
                        _handleNotification(_n,widget.items),child: DraggableScrollbar.arrows(
                      alwaysVisibleScrollThumb: true,controller: scrollController,child: ListView.builder(
                        controller: scrollController,itemCount: widget.items.length,itemBuilder: (context,index) {
                          return GestureDetector(
                            child: Padding(
                              padding: EdgeInsets.all(16),child: Align(
                                  alignment: Alignment.center,child: Text(
                                    widget.items[index],style: TextStyle(color: Colors.white),)),);
                        },)
            ],);
  }
}

如果我需要不结束会话?

解决方法

您可以使用tmux send-keys命令来实现此目的,该命令可以从命令行(或脚本)向现有会话发送命令:

tmux new-session -s test -d                       # Creates a session named 'test' and stays detached.
tmux send-keys -t test "python3 --version" Enter  # Sends the command you wanted to the session.
                                                  # Here,the command was executed and the session is still alive.
tmux attach -t test                               # (Optional) If you want to attach to your session after the command was executed.

如果要发送的命令在脚本中,则可以将该脚本的执行作为命令发送:

如果脚本为python_ver.sh(确保脚本可执行):

#!/bin/sh

python3 --version

那你就去

tmux new-session -s test -d                       # Creates a session named 'test' and stays detached.
tmux send-keys -t test "./python_ver.sh" Enter    # Sends the command you wanted to the session.
                                                  # Here,the command was executed and the session is still alive.
tmux attach -t test                               # (Optional) If you want to attach to your session after the command was executed.