Centos 下使用 Jvppeteer · fanyong920/jvppeteer Wiki (github.com)
error while loading shared libraries: libXss.so.1: cannot open shared object file: No such file or directory
场景:
linux centos8.2服务器;
docker容器运行的微服务facereport;
微服务facereport中用了jvppeteer技术调用chrome截图,如图:
前端调用微服务后报错,错误就是puppeteer启动chrome失败,说是加载共享库时,找不到。
注:puppeteer是前端爬虫工具,你可以理解为语法是js。 jvppeteer也是爬虫工具,语法是java。
问题:
linux上,使用 jvppeteer 启动chrome报错:
2021-02-24 08:49:30.534 INFO 1 --- [-nio-93-exec-10] c.r.j.core.browser.BrowserFetcher:
revision:722234,
executablePath:/.local-browser/linux-722234/chrome-linux/chrome,
folderPath:/.local-browser/linux-722234,
local:true,
url:https://npm.taobao.org/mirrors/chromium-browser-snapshots/Linux_x64/722234/chrome-linux.zip,
product:chrome
com.ruiyun.jvppeteer.exception.TimeoutException: Timed out after 30000 ms while trying to connect to the browser!
Chrome output: /.local-browser/linux-722234/chrome-linux/chrome: error while loading shared libraries: libXss.so.1: cannot open shared object file: No such file or directory
原因:
关键在于最后:
error while loading shared libraries: libXss.so.1: cannot open shared object file: No such file or directory
找不到这个叫作 libXss.so.1 的依赖。
解决过程:
1.查找chrome安装路径:
find / -name chrome
[root@cumt chrome-linux]# find / -name chrome
find: ‘/proc/4138391’: No such file or directory
find: ‘/proc/4138392’: No such file or directory
/var/lib/selinux/targeted/active/modules/100/chrome
/var/lib/docker/overlay2/562dbdf18ef7e71663da04c38e7108e4dbd1d9fab3dedc32d7d16d6624b2c08e/diff/.local-browser/linux-722234/chrome-linux/chrome
/var/lib/docker/overlay2/562dbdf18ef7e71663da04c38e7108e4dbd1d9fab3dedc32d7d16d6624b2c08e/merged/.local-browser/linux-722234/chrome-linux/chrome
/usr/share/selinux/targeted/default/active/modules/100/chrome
2.查看缺少的依赖
cd /var/lib/docker/overlay2/562dbdf18ef7e71663da04c38e7108e4dbd1d9fab3dedc32d7d16d6624b2c08e/merged/.local-browser/linux-722234/chrome-linux/
ldd chrome | grep not
查看缺少的依赖
3.安装缺少的依赖
(1). error while loading shared libraries: libatk-bridge-2.0.so.0
解决:
yum install at-spi2-atk -y
(2). error while loading shared libraries: libXss.so.1
解决:
yum install libXScrnSaver* -y
(3). error while loading shared libraries: libgtk-3.so.0
解决:
yum install gtk3 -y
(4). error while loading shared libraries: libasound.so.2: cannot open shared object file: No such file or directory
yum install nss
(5).error while loading shared libraries: libasound.so.2: cannot open shared object file: No such file or directory
yum install alsa-lib
其他缺少的依赖,到这查找
https://pkgs.org/search/?q=libasound.so.2
4.执行
ldconfig
刷新一下动态链接库缓存。(此处参考:https://blog.csdn.net/sahusoft/article/details/7388617)5.测试:
执行
./chrome
,如果不报缺少xxx动态链接库依赖的错误就是测试通过了,如图:它说的“Running as root without --no-sandbox is not supported”这玩意不用管,反正是不缺少依赖了,测试成功。
然后,我重新启动微服务facereport。前端调,还是报“error while loading shared libraries: libXss.so.1: cannot open shared object file: No such file or directory”坑啊。
针对上面的现象,想想:
首先,微服务是跑在docker里的,然后调用chrome的时候,需要下载chrome(因为上面代码中有:BrowserFetcher.downloadIfNotExist(null);
),下载的chrome是在这个路径下:/var/lib/docker/overlay2/562dbdf18ef7e71663da04c38e7108e4dbd1d9fab3dedc32d7d16d6624b2c08e/merged/.local-browser/linux-722234/chrome-linux/chrome
那么,我来到这个路径下:cd /var/lib/docker/overlay2/562dbdf18ef7e71663da04c38e7108e4dbd1d9fab3dedc32d7d16d6624b2c08e/merged
并ls
一下,发现是个虚拟机。没错,这是docker容器里的东西,docker这东西不就是个独立的麻雀嘛,五脏俱全。chrome每次都是被程序装到麻雀身体里的...怪不得它找不到我外面的链接库。
解决办法:把facereport从docker里挪出来启动,直接用java -jar xxx 启动。
测试成功,如图:
要特别注意这个executablePath,这次程序把chrome给我安装在了真正的服务器上,而不是docker的虚拟机里。